<template>
  <div class="borderless">
    <div class="customer-head">
      <div class="customer-back">
        <el-button
          type="danger"
          style="border: none;background: none;color: red"
          icon="el-icon-arrow-left"
          @click="backToList"
          >返回列表
        </el-button>
      </div>
      <el-button
        class="el-button--small-yh add-customer-btn"
        type="primary"
        :disabled="detailData.status == 1"
        size="small"
        @click="editCustomer"
        >保存数据
      </el-button>
    </div>
    <div class="customer-main">
       <containerTitle title="基础资料"></containerTitle>
    <basic-container>
      <avue-form class="trading-form" ref="form" v-model="form" :option="option" />
    </basic-container>
    <containerTitle title="基础明细"></containerTitle>
    <basic-container>
      <avue-crud
        ref="crud"
        :option="optionList"
        :data="dataList"
        :search.sync="search"
        @search-change="searchChange"
        @saveColumn="saveColumn"
        :cell-style="cellStyle"
        :header-cell-class-name="headerCellClassName"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            @click="dataList.push({ $cellEdit: true })"
            size="small"
            :disabled="detailData.status == 1"
            >新增</el-button
          >
          <el-button
            type="primary"
            size="small"
            icon="el-icon-bottom"
            @click="importExcel()"
            :disabled="detailData.status == 1"
            >导入
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-bottom"
            @click.stop="openExport()"
            >导出
          </el-button>
        </template>
        <template slot-scope="{ row, index }" slot="menu">
          <el-button
            type="text"
            size="small"
            @click.stop="rowCell(row)"
            :disabled="detailData.status == 1"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click.stop="rowDel(row, index)"
            :disabled="detailData.status == 1"
          >
            删除
          </el-button>
        </template>
      </avue-crud>
      <el-dialog
        title="导入教职工资料"
        append-to-body
        :visible.sync="excelBox"
        width="555px"
        v-dialog-drag
      >
        <avue-form
          :option="excelOption"
          v-model="excelForm"
          table-loading="excelLoading"
          :upload-after="uploadAfter"
        >
          <template slot="excelTemplate">
            <el-button type="primary" @click="derivation">
              点击下载<i class="el-icon-download el-icon--right"></i>
            </el-button>
          </template>
        </avue-form>
        <p style="text-align: center;color: #DC0505">
          温馨提示 第一次导入时请先下载模板
        </p>
      </el-dialog>
    </basic-container>
    </div>
  </div>
</template>

<script>
import { getToken } from "@/util/auth";
import { detail, submit, delItem } from "@/api/salaryManagement/primarySchool";
export default {
  name: "detailsPage",
  data() {
    return {
      excelBox: false,
      xcelLoading: false,
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24
          },
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data"
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/blade-school/salaryitem/import"
          }
        ]
      },
      form: {},
      dataList: [],
      allDataList: [],
      drawer: false,
      tableHeight: 0,
      option: {
        menuBtn: false,
        labelWidth: 100,
        column: [
          {
            label: "年",
            prop: "annual",
            type: "year",
            valueFormat: "yyyy",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 6,
            change: ({ value }) => {
              if (value) {
                this.optionList.column.forEach(e => {
                  if (e.prop == "yearUpPerformance") {
                    e.label = value + "年上学期绩效";
                  }
                  if (e.prop == "yearDownPerformance") {
                    e.label = value + "年下学期绩效";
                  }
                });
              }
            }
          },
          {
            label: "月",
            prop: "moon",
            type: "select",
            filterable: true,
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=month",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            span: 6,
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            change: ({ value }) => {
              if (value) {
                this.optionList.column.forEach(e => {
                  if (e.prop == "monthHour") {
                    e.label = value + "月份课时";
                  }
                  if (e.prop == "momthWorkloadSalary") {
                    e.label = value + "月工作量工资";
                  }
                  if (e.prop == "monthViceClassFee") {
                    e.label = value + "月副班费（预留补发）";
                  }
                });
              }
            }
          },
          {
            label: "备注",
            prop: "remarks",
            type: "textarea",
            span: 12,
            minRows: 2
          }
        ]
      },
      optionList: {
        searchMenuSpan: 16,
        addBtn: false,
        border: true,
        align: "center",
        index: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        refreshBtn: false,
        menuWidth: 100,
        column: [
          {
            label: "姓名",
            prop: "cname",
            width: 100,
            searchSpan: 8,
            search: false,
            cell: true
          },
          {
            label: "职级拨付标准",
            prop: "rankWithdrawalStandard",
            width: 100,
            cell: true
          },
          {
            label: "身份证号",
            prop: "idNumber",
            width: 100,
            cell: true
          },
          {
            label: "学科",
            prop: "discipline",
            width: 100,
            cell: true
          },
          {
            label: "类别",
            prop: "itemType",
            width: 100,
            cell: true
          },
          {
            label: "转正时间",
            prop: "positiveTime",
            type: "date",
            valueFormat: "yyyy-MM-dd",
            width: 100,
            cell: true
          },
          {
            label: "职务",
            prop: "position",
            width: 100,
            cell: true
          },
          {
            label: "工资拨付标准",
            prop: "salaryWithdrawalStandard",
            width: 100,
            cell: true
          },
          {
            label: "履约奖",
            prop: "performanceAward",
            width: 100,
            cell: true
          },
          {
            label: "校龄工资",
            prop: "schoolAgeSalary",
            width: 100,
            cell: true
          },
          {
            label: "增资",
            prop: "capitalIncrease",
            width: 100,
            cell: true
          },
          {
            label: "学历工资",
            prop: "educationSalary",
            width: 100,
            cell: true
          },
          {
            label: "职级标准",
            prop: "rankStandard",
            width: 100,
            cell: true
          },
          {
            label: "职级工资",
            prop: "rankSalary",
            width: 100,
            cell: true
          },
          {
            label: "工资基数",
            prop: "salaryBase",
            width: 100,
            cell: true
          },
          {
            label: "基础工资",
            prop: "basicsSalary",
            width: 100,
            cell: true
          },
          {
            label: "工作量工资标准",
            prop: "workloadSalaryCritertion",
            width: 120,
            cell: true
          },
          {
            label: "月份课时",
            prop: "monthHour",
            width: 100,
            cell: true
          },
          {
            label: "月工作量工资",
            prop: "momthWorkloadSalary",
            width: 120,
            cell: true
          },
          {
            label: "自定义",
            prop: "custom",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customOne",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customTwo",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customThree",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customFour",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customFive",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customSix",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customSeven",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customEight",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customNine",
            width: 100,
            cell: true
          },
          {
            label: "工作量工资",
            prop: "workloadSalary",
            width: 100,
            cell: true
          },
          {
            label: "日常考核标准",
            prop: "dailyAssessmentCriteria",
            width: 100,
            cell: true
          },
          {
            label: "日常考核工资",
            prop: "dailyAssessmentSalary",
            width: 100,
            cell: true
          },
          {
            label: "学期绩效工资",
            prop: "termPerformanceSalary",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAward",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardOne",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardTwo",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardThree",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardFour",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardFive",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardSix",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardSeven",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardEight",
            width: 100,
            cell: true
          },
          {
            label: "自定义奖励",
            prop: "customAwardNine",
            width: 100,
            cell: true
          },
          {
            label: "奖励工资",
            prop: "awardSalary",
            width: 100,
            cell: true
          },
          {
            label: "领导干部补贴",
            prop: "leadingCadreSubsidy",
            width: 100,
            cell: true
          },
          {
            label: "备课组长",
            prop: "prepareLessons",
            width: 100,
            cell: true
          },
          {
            label: "教研组长",
            prop: "teachingResearch",
            width: 100,
            cell: true
          },
          {
            label: "职务津贴",
            prop: "dutyAllowances",
            width: 100,
            cell: true
          },
          {
            label: "班主任费",
            prop: "classTeacherFee",
            width: 100,
            cell: true
          },
          {
            label: "班额",
            prop: "classes",
            width: 100,
            cell: true
          },
          {
            label: "班主任津贴",
            prop: "classTeacherSubsidy",
            width: 100,
            cell: true
          },
          {
            label: "微信",
            prop: "wx",
            width: 100,
            cell: true
          },
          {
            label: "产假工资",
            prop: "maternityLeaveSalary",
            width: 100,
            cell: true
          },
          {
            label: "周末加班",
            prop: "overtimeAtWeekend",
            width: 100,
            cell: true
          },
          {
            label: "年上学期绩效",
            prop: "yearUpPerformance",
            width: 130,
            cell: true
          },
          {
            label: "年下学期绩效",
            prop: "yearDownPerformance",
            width: 130,
            cell: true
          },
          {
            label: "干部补贴预留",
            prop: "cadreAllowanceReservation",
            width: 100,
            cell: true
          },
          {
            label: "假期加班费",
            prop: "vacationOvertimePay",
            width: 100,
            cell: true
          },
          {
            label: "新岗岗前培训",
            prop: "newPostTrain",
            width: 100,
            cell: true
          },
          {
            label: "期中期末考试学科优胜奖（预留发放）",
            prop: "winningPrize",
            width: 240,
            cell: true
          },
          {
            label: "级部长（预留发放）",
            prop: "classMinister",
            width: 200,
            cell: true
          },
          {
            label: "月副班费（预留补发）",
            prop: "monthViceClassFee",
            width: 160,
            cell: true
          },
          {
            label: "餐车、周五测温、周五安全岗（预留补发）",
            prop: "safetyFees",
            width: 250,
            cell: true
          },
          {
            label: "预留补发",
            prop: "reserveReplacement",
            width: 100,
            cell: true
          },
          {
            label: "调整差额",
            prop: "adjustTheBalance",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customize",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeOne",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeTwo",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeThree",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeFour",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeFive",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeSix",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeSeven",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeEight",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "customizeNine",
            width: 100,
            cell: true
          },
          {
            label: "补贴项",
            prop: "subsidy",
            width: 100,
            cell: true
          },
          {
            label: "出勤",
            prop: "attendance",
            width: 100,
            cell: true
          },
          {
            label: "督查罚款",
            prop: "inspectorFines",
            width: 100,
            cell: true
          },
          {
            label: "电话费",
            prop: "telephoneBill",
            width: 100,
            cell: true
          },
          {
            label: "学习强国",
            prop: "learningPower",
            width: 100,
            cell: true
          },
          {
            label: "宿舍管理费",
            prop: "dormitoryManagementFee",
            width: 100,
            cell: true
          },
          {
            label: "生育津贴",
            prop: "childbirthAllowance",
            width: 100,
            cell: true
          },
          {
            label: "五险一金个人",
            prop: "androidIt",
            width: 100,
            cell: true
          },
          {
            label: "扣除项",
            prop: "deductions",
            width: 100,
            cell: true
          },
          {
            label: "保险（单位部分）",
            prop: "insuranceUnit",
            width: 120,
            cell: true
          },
          {
            label: "保险（个人部分）",
            prop: "insurancePersonage",
            width: 120,
            cell: true
          },
          {
            label: "公积金（单位部分）",
            prop: "reservedFundsUnit",
            width: 140,
            cell: true
          },
          {
            label: "公积金（个人部分）",
            prop: "reservedFundsPersonage",
            width: 140,
            cell: true
          },
          {
            label: "应发工资",
            prop: "salary",
            width: 100,
            cell: true
          },
          {
            label: "个税",
            prop: "personageTaxes",
            width: 100,
            cell: true
          },
          {
            label: "实发工资（减去个人）",
            prop: "fsalary",
            width: 150,
            cell: true
          },
          {
            label: "备注",
            prop: "remarks",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbon",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonOne",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonTwo",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonThree",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonFour",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonFive",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonSix",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonSeven",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonEight",
            width: 100,
            cell: true
          },
          {
            label: "自定义",
            prop: "ribbonNine",
            width: 100,
            cell: true
          },
          {
            label: "日照银行代发最终",
            prop: "payrollCredit",
            width: 120,
            cell: true
          },
          {
            label: "已付",
            prop: "paid",
            width: 100,
            cell: true
          },
          {
            label: "冲借款",
            prop: "rushedToBorrow",
            width: 100,
            cell: true
          },
          {
            label: "现金付款",
            prop: "cashPayment",
            width: 100,
            cell: true
          },
          {
            label: "暂不付",
            prop: "pauseNoPay",
            width: 100,
            cell: true
          },
          {
            label: "暂不付1",
            prop: "pauseNoPayOne",
            width: 100,
            cell: true
          },
          {
            label: "暂不付2",
            prop: "pauseNoPayTwo",
            width: 100,
            cell: true
          },
          {
            label: "暂不付3",
            prop: "pauseNoPayThree",
            width: 100,
            cell: true
          },
          {
            label: "暂不付4",
            prop: "pauseNoPayFour",
            width: 100,
            cell: true
          },

          {
            label: "暂不付5",
            prop: "pauseNoPayFive",
            width: 100,
            cell: true
          },
          {
            label: "暂不付6",
            prop: "pauseNoPaySix",
            width: 100,
            cell: true
          },
          {
            label: "暂不付7",
            prop: "pauseNoPaySeven",
            width: 100,
            cell: true
          },
          {
            label: "暂不付8",
            prop: "pauseNoPayEight",
            width: 100,
            cell: true
          },
          {
            label: "暂不付9",
            prop: "pauseNoPayNine",
            width: 100,
            cell: true
          },
          {
            label: "备注",
            prop: "pauseNoPayRemark",
            width: 100,
            cell: true
          }
        ]
      }
    };
  },
  props: {
    detailData: {
      type: Object
    }
  },
  created() {
    if (this.detailData.id) {
      this.getDetail(this.detailData.id);
    }
    if (this.detailData.status == 1) {
      this.option.disabled = true;
      this.findObject(this.optionList.column, "cname").search=true
    }
    this.optionList.height = window.innerHeight - 380;
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    headerCellClassName({ row, column, rowIndex, columnIndex }) {
      if (
        column.property == "salaryBase" ||
        column.property == "workloadSalary" ||
        column.property == "dailyAssessmentSalary" ||
        column.property == "awardSalary" ||
        column.property == "dutyAllowances" ||
        column.property == "classTeacherSubsidy" ||
        column.property == "subsidy" ||
        column.property == "deductions" ||
        column.property == "insuranceUnit" ||
        column.property == "insurancePersonage" ||
        column.property == "reservedFundsUnit" ||
        column.property == "reservedFundsPersonage" ||
        column.property == "salary" ||
        column.property == "personageTaxes" ||
        column.property == "fsalary"
      ) {
        return "headerCellClass";
      }
    },
    getDetail(id) {
      detail(id)
        .then(res => {
          this.form = res.data.data;
          this.dataList = res.data.data.salaryItemList;
          if (this.detailData.status == 1) {
            this.allDataList = res.data.data.salaryItemList;
          }
        })
        .finally(() => {
          // this.loading = false;
          // this.showBut = true;
          // this.pageLoading = false;
        });
    },
    rowCell(row, index) {
      if (row.$cellEdit == true) {
        this.$set(row, "$cellEdit", false);
      } else {
        this.$set(row, "$cellEdit", true);
      }
    },
    rowDel(row, index) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (row.id) {
          delItem(row.id).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.dataList.splice(index, 1);
          });
        } else {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.dataList.splice(index, 1);
        }
      });
    },
    searchChange(params, done) {
      if (params.cname) {
        this.dataList = this.allDataList.filter(e => e.cname == params.cname);
      } else {
        this.dataList = this.allDataList;
      }
      done();
    },
    uploadAfter(res, done, loading, column) {
      this.excelBox = false;
      this.$message.success("导入成功！");
      if (this.detailData.id) {
        this.getDetail(this.detailData.id);
      }
      done();
    },
    //修改提交触发
    editCustomer() {
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          for (let i = 0; i < this.dataList.length; i++) {
            if (!this.dataList[i].cname) {
              return this.$message.error(
                "请完善第" + Number(1 + i) + "行的姓名"
              );
            }
            if (!this.dataList[i].idNumber) {
              return this.$message.error(
                "请完善第" + Number(1 + i) + "行的身份证号"
              );
            }
            if (!this.dataList[i].rankStandard) {
              return this.$message.error(
                "请完善第" + Number(1 + i) + "行的职级标准"
              );
            }
          }
          submit({ ...this.form, salaryItemList: this.dataList })
            .then(res => {
              this.$message.success("保存成功");
              this.form = res.data.data;
              this.dataList = res.data.data.salaryItemList;
            })
            .finally(() => {});
        } else {
          return false;
        }
      });
    },
    derivation() {
      window.open(
        `/api/blade-school/salaryitem/export/template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    openExport() {
      if (!this.form.id) {
        return this.$message.error("请先保存数据");
      }
      window.open(
        `/api/blade-school/salaryitem/export/teacher?${
          this.website.tokenHeader
        }=${getToken()}&pid=${this.form.id}`
      );
    },
    importExcel() {
      if (!this.form.id) {
        return this.$message.error("请先保存数据");
      }
      this.excelOption.column.forEach(e => {
        if (e.prop == "excelFile") {
          e.data = {
            id: this.form.id
          };
        }
      });
      this.excelBox = true;
    },
    //导出全部
    exportAll() {
      let opt = {
        title: "工资条",
        column: this.optionList.column,
        data: this.dataList
      };
      this.$Export.excel({
        title: opt.title,
        columns: opt.column,
        data: opt.data
      });
    },
    //导出工资条
    exportSalary() {
      let data = [];
      for (let item in this.optionList.column) {
        switch (this.optionList.column[item].prop) {
          case "salaryBase":
            data.push(this.optionList.column[item]);
            break;
          case "workloadSalary":
            data.push(this.optionList.column[item]);
            break;
          case "dailyAssessmentSalary":
            data.push(this.optionList.column[item]);
            break;
          case "awardSalary":
            data.push(this.optionList.column[item]);
            break;
          case "dutyAllowances":
            data.push(this.optionList.column[item]);
            break;
          case "classTeacherSubsidy":
            data.push(this.optionList.column[item]);
            break;
          case "subsidy":
            data.push(this.optionList.column[item]);
            break;
          case "deductions":
            data.push(this.optionList.column[item]);
            break;
          case "insuranceUnit":
            data.push(this.optionList.column[item]);
            break;
          case "insurancePersonage":
            data.push(this.optionList.column[item]);
            break;
          case "reservedFundsUnit":
            data.push(this.optionList.column[item]);
            break;
          case "reservedFundsPersonage":
            data.push(this.optionList.column[item]);
            break;
          case "salary":
            data.push(this.optionList.column[item]);
            break;
          case "personageTaxes":
            data.push(this.optionList.column[item]);
            break;
          case "fsalary":
            data.push(this.optionList.column[item]);
            break;
        }
      }
      let opt = {
        title: "工资条",
        column: data,
        data: this.dataList
      };
      this.$Export.excel({
        title: opt.title,
        columns: opt.column,
        data: opt.data
      });
    },
    //返回列表
    backToList() {
      this.$emit("goBack");
    }
  }
};
</script>

<style>
.headerCellClass {
  color: #fff !important;
  background: #a4cf57 !important;
}
</style>
<style lang="scss" scoped>
.trading-form ::v-deep .el-form-item {
  margin-bottom: 0px !important;
}
::v-deep .el-form-item__error {
  display: none !important;
}
</style>