<template>
  <div>
    <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"
          size="small"
          @click="editCustomer"
          >保存数据
        </el-button>
      </div>
      <containerTitle
        title="基础资料"
        style="margin-top: 60px"
      ></containerTitle>
      <basic-container>
        <avue-form
          ref="form"
          class="trading-form"
          v-model="form"
          :option="option"
        />
      </basic-container>
      <containerTitle title="事业编考试的记录"></containerTitle>
      <basic-container>
        <avue-crud
          ref="crud"
          :option="option2"
          :data="dataList"
          :table-loading="loading"
          @saveColumn="saveColumn"
          :cell-style="cellStyle"
        >
          <template slot="menuLeft">
            <el-button
              type="primary"
              @click="dataList.push({ $cellEdit: true })"
              size="small"
              :disabled="detailData.status == 1"
              >新增
            </el-button>
          </template>
          <template slot="menu" slot-scope="{ row, index }">
            <el-button
              size="small"
              type="text"
              @click="rowCell(row, index)"
              :disabled="detailData.status == 1"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="text"
              @click="rowDel(row, index)"
              :disabled="detailData.status == 1"
              >删除</el-button
            >
          </template>
        </avue-crud>
      </basic-container>
    </div>
  </div>
</template>

<script>
import { detail, submit, delItem } from "@/api/basicData/facultyManagement";
export default {
  name: "index",
  data() {
    return {
      form: {},
      dataList: [],
      option: {
        menuBtn: false,
        labelWidth: 100,
        column: [
          {
            label: "姓名",
            prop: "cname",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 6
          },
          {
            label: "身份证",
            prop: "idNumber",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 6
          },
          {
            label: "性别",
            prop: "gender",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=sex",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            span: 6
          },
          {
            label: "联系电话",
            prop: "phone",
            span: 6
          },
          {
            label: "学历",
            prop: "code",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=marital_status",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            span: 6
          },
          {
            label: "入职时间",
            prop: "joiningDate",
            type: "date",
            valueFormat: "yyyy-MM-dd",
            span: 6
          },
          {
            label: "转正时间",
            prop: "birthdate",
            type: "date",
            valueFormat: "yyyy-MM-dd",
            span: 6
          },
          {
            label: "校龄开始日期",
            prop: "schoolAgeSalaryDate",
            type: "date",
            valueFormat: "yyyy-MM-dd",
            span: 6
          },
          {
            label: "校龄",
            prop: "schoolAge",
            type: "number",
            controls: false,
            precision:0,
            minRows: 0,
            span: 6
          },
          {
            label: "所在部门",
            prop: "inSection",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=in_section",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 6
          },
          {
            label: "是否班主任",
            prop: "doubleTeachers",
            type: "radio",
            dataType: "string",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=TF_status",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            span: 6
          },
          {
            label: "职务",
            prop: "compileCategory",
            type: "select",
            dicUrl:
              "/api/blade-system/dict-biz/dictionary?code=compile_category",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            span: 6
          },
          {
            label: "备注",
            prop: "remarks",
            type: "textarea",
            span: 24,
            minRows: 2
          }
        ]
      },
      option2: {
        align: "center",
        addBtn: false,
        refreshBtn: false,
        editBtn: false,
        delBtn: false,
        border: true,
        menuWidth: 120,
        column: [
          {
            label: "考试日期",
            prop: "birthdate",
            type: "date",
            valueFormat: "yyyy-MM-dd",
            cell: true
          },
          {
            label: "备注",
            prop: "remarks",
            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;
    }
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    getDetail(id) {
      detail(id)
        .then(res => {
          this.form = res.data.data;
          this.dataList = res.data.data.causeList;
        })
        .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);
        }
      });
    },
    //修改提交触发
    editCustomer() {
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          submit({ ...this.form, causeList: this.dataList })
            .then(res => {
              this.$message.success("保存成功");
              this.form = res.data.data;
              this.dataList = res.data.data.causeList;
            })
            .finally(() => {});
        } else {
          return false;
        }
      });
    },
    //返回列表
    backToList() {
      this.$emit("goBack");
    }
  }
};
</script>

<style lang="scss" scoped>
.trading-form ::v-deep .el-form-item {
  margin-bottom: 8px !important;
}
::v-deep .el-form-item__error {
  display: none !important;
}
.img-form ::v-deep .el-form-item {
  height: 150px;
  line-height: 150px;
  margin-bottom: 8px !important;
}
.img-form ::v-deep .avue-upload__icon {
  font-size: 20px;
  width: 150px;
  height: 150px;
  line-height: 150px;
}
</style>
