<template>
  <div class="manage">
    <!-- <TitleBar propTitle="收入管理" /> -->
    <div class="add-container">
      <el-button type="primary" @click="append('INCOME')">增加收入</el-button>
    </div>
    <el-card class="right-card">
      <el-table :data="manageData" style="width: 100%" :row-class-name="tableRowClassName">
        <!-- <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="备注">
                <span>{{ props.row.remark ? props.row.remark : '无' }}</span>
              </el-form-item>
              <el-form-item label="接收部门">
                <span>{{ props.row.toDepartment.name ? props.row.toDepartment.name : '无' }}</span>
              </el-form-item>
              <el-form-item label="接受项目">
                <span>{{ props.row.toProject.name ? props.row.toProject.name : '无' }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column> -->
        <el-table-column label="发生日期" sortable :formatter="dateFormat" prop="happenTime"></el-table-column>
        <el-table-column label="金额">
          <template slot-scope="props">
            <span>{{ props.row.type == 'COST' ? '-' + props.row.amount : '+' + props.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="收入状态">
          <template slot-scope="props">
            <span>{{ props.row.incomeStatus == 'INVOICE' ? '开票/应收' : props.row.incomeStatus == 'ARRIVAL' ? '到账' : '内部核算' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="产品" prop="product.name"></el-table-column>
        <el-table-column label="归属部门" prop="department.name"></el-table-column>
        <el-table-column label="归属项目" prop="project.name"></el-table-column>
        
        <!-- <el-table-column label="接收部门" prop="toDepartment.name"></el-table-column> -->
        <!-- <el-table-column label="接受项目" prop="toProject.name"></el-table-column> -->
        <el-table-column label="备注" prop="remark"></el-table-column>
        
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <!--
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            -->
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="type == 'INCOME' ? '收入' : '成本'" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item
          label="发生时间"
          prop="happenTime"
          :label-width="formLabelWidth"
          :rules="[{ required: true, message: '名称不能为空'}]"
        >
          <!--<el-input v-model="form.happenTime" autocomplete="off" placeholder="请输入格式2019/00/00"></el-input>-->
          <el-date-picker v-model="form.happenTime" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item
          label="金额"
          prop="amount"
          :label-width="formLabelWidth"
          :rules="[
          { required: true, message: '编码不能为空'}, 
          // { type: 'float', message: '金额必须为数字值'}
        ]"
        >
          <el-input style="width: 35%;" v-model="form.amount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="收入状态"
          prop="incomeStatus"
          :label-width="formLabelWidth"
          :rules="[{ required: true, message: '状态不能为空'}]"
        >
          <el-select v-model="form.incomeStatus" placeholder="请选择状态值">
            <el-option key label="无" value></el-option>
            <el-option label="开票/应收" value="INVOICE"></el-option>
            <el-option label="到账" value="ARRIVAL"></el-option>
            <el-option label="内部核算" value="INTERNAL"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="归属产品" prop="productId" :label-width="formLabelWidth">
          <el-select v-model="form.productId" placeholder="请选择产品">
            <el-option key label="无" value></el-option>
            <el-option
              v-for="item in productSelectData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归属项目" prop="projectId" :label-width="formLabelWidth">
          <el-select v-model="form.projectId" placeholder="请选择项目">
            <el-option key label="无" value></el-option>
            <el-option
              v-for="item in projectSelectData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="来源部门" prop="toDeptId" :label-width="formLabelWidth">
          <el-select v-model="form.toDeptId" placeholder="请选择部门">
            <el-option key label="无" value></el-option>
            <el-option
              v-for="item in teamSelectData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="来源项目" prop="toProjectId" :label-width="formLabelWidth">
          <el-select v-model="form.toProjectId" placeholder="请选择项目">
            <el-option key label="无" value></el-option>
            <el-option
              v-for="item in projectSelectData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="归属部门" prop="deptId" :label-width="formLabelWidth">
          <el-select v-model="form.deptId" placeholder="请选择部门" ref="select" @change="getName">
            <el-option key label="无" value></el-option>
            <el-option
              v-for="item in teamSelectData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="添加备注" prop="remark" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="2" v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTeamSelect,
  getProjectSelect,
  getProductSelect,
  getCostTeamSelect,
  getCostTypeSelect,
  addManage,
  getManageList,
  setManage
} from "@/api/manageApi";
import TitleBar from '../layout/titleBar/TitleBar.vue';
import {formatTime} from '../../utils/common';
export default {
  components: {
    TitleBar
  },
  data() {
    return {
      type: "",
      dialogFormVisible: false,
      addFlag: false,
      id: "",
      form: {
        deptId: "",
        projectId: "",
        costTypeId: "",
        companyId: "",
        companyType: "",
        happenTime: "",
        incomeStatus: "",
        productId: "",
        toProjectId: "",
        remark: "",
        amount: "",
        toDeptId: "",
        deptName: "",
        code: "",
        name: "",
        status: "NORMAL",
        sort: ""
      },
      formLabelWidth: "120px",
      teamSelectData: [],
      projectSelectData: [],
      productSelectData: [],
      costTeamSelectData: [],
      costTypeSelectData: [],
      manageData: [],
      companyData: []
    };
  },
  created() {
    // 获取部门下拉框
    getTeamSelect().then(res => {
      this.teamSelectData = res.data;
    });
    // 获取项目下拉框
    getProjectSelect().then(res => {
      this.projectSelectData = res.data;
    });
    //获取产品下拉框
    getProductSelect().then(res => {
      this.productSelectData = res.data;
    });
    //获取经营列表
    getManageList({
      type: "INCOME"
    }).then(res => {
      this.manageData = res.data;
    });
    //获取单位下拉
    getCostTeamSelect().then(res => {
      this.costTeamSelectData = res.data;
    });
    //获取成本类型下拉
    getCostTypeSelect().then(res => {
      this.costTypeSelectData = res.data;
    });
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (row.type === "COST") {
        return "cost-row";
      } else {
        return "income-row";
      }
      return "";
    },
    //获取部门名称
    getName(val) {
      let obj = {};
      obj = this.teamSelectData.find(item => {
        return item.id === val;
      });
      let getName = "";
      this.form.deptName = obj.name;
    },
    // 添加
    append(type) {
      this.type = type;
      this.dialogFormVisible = true;
      this.addFlag = true;
    },
    // 编辑
    handleEdit(index, row) {
      console.log(row);
      //回显赋值
      this.type = row.type;
      this.form.deptId = row.department ? row.department.id: '';
      this.form.projectId = row.project ? row.project.id : '';
      this.form.code = row.code;
      this.form.happenTime = row.happenTime;
      this.form.incomeStatus = row.incomeStatus;
      this.form.productId = row.productId;
      this.form.remark = row.remark;
      this.form.amount = row.amount;
      this.form.name = row.name;
      // this.form.status = row.status;
      this.form.sort = row.sort;
      this.form.costTypeId = row.costTypeId;
      this.form.companyId = row.companyId;
      this.form.companyType = row.companyType;
      this.id = row.id;
      this.dialogFormVisible = true;
      this.addFlag = false;
    },
    // 提交新增
    submitForm(formName) {
      //验证表单
      this.$refs[formName].validate((valid, value) => {
        if (valid) {
          this.form.happenTime = new Date(this.form.happenTime) * 1;
          this.form.type = this.type;
          if (this.addFlag) {
            addManage(this.form).then(res => {
              console.log(res);
              //获取经营列表
              getManageList({
                type: "INCOME"
              }).then(res => {
                this.manageData = res.data;
              });
              this.$refs[formName].resetFields();
            });
          } else {
            this.form.id = this.id;
            setManage(this.form).then(res => {
              //获取经营列表
              getManageList({
                type: "INCOME"
              }).then(res => {
                this.manageData = res.data;
              });
              this.$refs[formName].resetFields();
            });
          }
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    },
    // 格式化时间
    dateFormat(row, column) {
      return formatTime(row.happenTime)
    },
  }
};
</script>

<style scope>
.manage {
  padding: 20px;
   /* padding-top: 0; */
  box-sizing: border-box;
}
.add-container {
  float: right;
  margin-bottom: 20px;
}
.right-card {
  width: 100%;
  margin: 0 auto;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-table .cost-row {
  color: green;
}

.el-table .income-row {
  color: red;
}
</style>

