<template>
  <el-row>
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="dataSource"
        :page.sync="page"
        ref="crud"
        v-model="crudValues"
        :permission="permissionList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @current-change="currentChange"
        @size-change="sizeChange"
        class="customPage"
        :before-open="beforeOpen"
      >
        <template slot-scope="{row}" slot="employInfoListForm">
          <div>
            <el-radio-group v-model="employInfoType" @input="employInfoInput">
              <el-radio-button :label="1">失业就业信息</el-radio-button>
              <el-radio-button :label="2">养老待遇信息</el-radio-button>
              <el-radio-button :label="3">人员死亡信息</el-radio-button>
            </el-radio-group>
            <el-table
              v-if="employInfoType === 1"
              :data="row.employInfoList"
              style="width: 100%">
              <el-table-column
                prop="workCompany"
                label="工作单位">
              </el-table-column>
              <el-table-column
                prop="date"
                label="日期"
                width="180">
              </el-table-column>
              <el-table-column
                prop="title"
                label="内容"
                width="180">
              </el-table-column>
              <el-table-column
                prop="remarks"
                label="备注">
              </el-table-column>
            </el-table>
            <div v-else-if="employInfoType === 2">
              <el-row style="margin-top: 10px">
                <el-col :span="8">养老保险: {{
                    row.isPension ? '已领取' : ' 未领取'
                  }}
                </el-col>
                <el-col :span="8">最大做账期: {{ splitDate(row.biggestDate) || '无' }}</el-col>
                <el-col :span="8">经办时间: {{
                    row.operateDate && dateFormat(new Date(Number(row.operateDate))) || '无'
                  }}
                </el-col>
                <el-col :span="8">开始年月: {{ splitDate(row.startDate) || '无' }}</el-col>
                <el-col :span="8">终止年月: {{ splitDate(row.endDate) || '无' }}</el-col>
              </el-row>
            </div>
            <div v-else-if="employInfoType === 3">
              <el-row style="margin-top: 10px">
                <el-col :span="8">是否死亡: {{ row.isDead ? '已死亡' : ' 未死亡' }}</el-col>
                <el-col :span="8">火化地址: {{ row.funeralAddress || '无' }}</el-col>
                <el-col :span="8">火化时间: {{ row.funeralDate || '无' }}</el-col>
              </el-row>
            </div>
          </div>
        </template>
        <!--        <template slot-scope="{row}" slot="menu">-->
        <!--          <div>-->
        <!--            <el-button type="text"-->
        <!--                       icon="el-icon-document"-->
        <!--                       size="small"-->
        <!--                       @click="serveLog(row)"-->
        <!--            >查看-->
        <!--            </el-button>-->
        <!--          </div>-->
        <!--        </template>-->
      </avue-crud>
    </basic-container>
  </el-row>
</template>

<script>
import {mapGetters} from "vuex";
import {getList} from "@/api/tenant/personnelserve";
import {isvalidatemobile} from "@/util/validate";
import {detail, getDept, getDeptMyTree, talentsDetail} from "@/api/tenant/talents";
import {addServeLog, getMainServeUserLog, removeServeLog, updateServeLog} from "@/api/tenant/serve";
import {dateFormat} from "@/util/date";

const page = {
  size: 10,
  currentPage: 1,
  total: 0,
}
const baseOptions = {
  size: 'medium',
  dateBtn: false,
  addBtn: false,
  editBtn: false,
  viewBtn: true,
  delBtn: false,
  height: "auto",
  menuWidth: 130,
  reserveSelection: false,
  border: true,
  columnBtn: false,
  refreshBtn: false,
  menu: true,
  tip: false,
  selection: false,
  align: 'center',
  searchMenuSpan: 6,
  dialogType: "drawer",
  searchLabelWidth: 60,
}
export default {
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 15) {
        return value.slice(0, 14) + "...";
      }
      return value;
    },
  },
  data() {
    return {
      employInfoType: 1,
      loading: false,
      dataSource: [],
      crudValues: {},
      depTree: [],
      page: Object.assign({}, page),
      treeData: [],
      logPage: Object.assign({}, page),
      logDataSource: [],
      loadingLog: false,
      innerDrawerLog: false,
      innerDrawerAddLog: false,
      logValues: {}
    }
  },
  computed: {
    ...mapGetters(["permission", "userInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.tenant_main_talents_latent_index_add, false),
        viewBtn: true,
        delBtn: this.vaildData(this.permission.tenant_main_talents_latent_index_del, false),
        editBtn: this.vaildData(this.permission.tenant_main_talents_latent_index_edit, false),
      };
    },
    option() {
      const self = this;
      const IdCardNo = (rule, value, callback) => {
        // if (check18IdCardNo(value)) {
        callback();
        // } else {
        //   callback(new Error("身份证格式不正确"));
        // }
      };
      const validateTel = (rule, value, callback) => {
        if (isvalidatemobile(value)[0]) {
          callback(new Error(isvalidatemobile(value)[1]));
        } else {
          callback();
        }
      };
      return {
        ...baseOptions,
        column: [
          {
            label: "所属机构",
            prop: "createDept",
            type: "tree",
            multiple: false,
            dicData: this.depTree,
            props: {
              label: "title",
              value: 'value'
            },
            checkStrictly: true,
            span: 24,
            search: true,
            display: false,
            searchLabelWidth: 80,
            change: this.deptChange
          },
          {
            label: "姓名",
            prop: "name",
            // search: true,
            span: 24,
            display: false,
            rules: [
              {
                required: true,
                whitespace: true,
                message: "请输入姓名",
                trigger: "blur",
              },
            ],
            search: true,
          },
          {
            label: "姓别",
            prop: "aac004",
            type: 'select',
            dicUrl: "/api/jobslink-api/system/dict-biz/tadict?code=AAC004",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            display: false,
            rules: [
              {
                required: true,
                message: "请输入性别",
                trigger: "blur",
              },
            ],
            span: 24,
          },
          {
            label: "身份证",
            prop: "idNumber",
            // hide: true,
            span: 24,
            display: false,
            rules: [
              {required: true, message: "请输入身份证号", trigger: "blur"},
              {trigger: "blur", validator: IdCardNo},
            ],
            search: true,
          },
          {
            label: "手机号",
            prop: "telphone",
            span: 24,
            display: false,
            hide: false,
            rules: [
              {
                required: true,
                validator: validateTel,
                trigger: "blur",
              },
            ],
          },
          {
            label: "民族",
            prop: "aac005",
            display: false,
            type: 'select',
            dicUrl: "/api/jobslink-api/system/dict-biz/tadict?code=AAC005",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请输入民族",
                trigger: "blur",
              },
            ],
            span: 24,
          },
          {
            label: "户口性质",
            display: false,
            prop: "aac009",
            type: 'select',
            dicUrl: "/api/jobslink-api/system/dict-biz/tadict?code=AAC009",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请输入户口性质",
                trigger: "blur",
              },
            ],
            span: 24,
          },
          {
            label: "户口所在地",
            prop: "aac010",
            // hide: true,
            span: 24,
            display: false,
            rules: [
              {required: true, message: "请输入口所在地", trigger: "blur"},
            ],
          },
          {
            label: "文化程度",
            prop: "aac011",
            type: 'select',
            display: false,
            dicUrl: "/api/jobslink-api/system/dict-biz/tadict?code=AAC011",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请输入文化程度",
                trigger: "blur",
              },
            ],
            span: 24,
          },
          {
            label: "备注",
            prop: "remarks",
            type: "textarea",
            slot: true,
            hide: true,
            display: false,
            span: 24,
            maxlength: 100,
            showWordLimit: true,
          },
          {
            label: '',
            prop: 'employInfoList',
            formslot: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
            hide: true,
            span: 24,
            labelPosition: 'top', //标签位置
          }
        ],
      }
    },
  },
  created() {
    this.getDept()
    this.initDept()
    this.onLoad(this.page, this.query)
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.crud.refreshTable()
    })
  },
  methods: {
    splitDate(time) {
      if (!time) return
      return `${time.slice(0, 4)}年${time.slice(4, 6)}日`
    },
    dateFormat: dateFormat,
    async getDept() {
      let params = {
        tenantId: '000000'
      }
      let resData = await getDeptMyTree(params)
      if (resData.data.code === 200) {
        this.depTree = resData.data.data
      }
    },
    initDept(groupId) {
      getDept({groupType: 0}).then((res) => {
        const data = res.data.data.list;
        this.treeData = data;
        let treeDataOne = {
          companyId: "",
          createTime: "",
          groupName: "全部",
          id: "",
          remarks: "",
          sort: '',
          sumNum: res.data.data.sum,
          type: '',
        };
        this.treeData.unshift(treeDataOne);
        this.loading = false;
      });
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.query);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page, this.query);
    },
    logSizeChange(size) {
      this.logPage.size = size;
      this.serveLog(this.selectUserServeLog);
    },
    logCurrentChange(currentPage) {
      this.logPage.currentPage = currentPage;
      this.serveLog(this.selectUserServeLog);
    },
    onLoad(page, params = {}) {
      const {releaseTimeRange} = params;
      let paramsed = {
        current: page.currentPage,
        size: page.size,
        ...this.query
      };
      this.loading = true;
      getList(paramsed).then((res) => {
        const {total, size, current, records, page} = res.data.data
        this.dataSource = records;
        this.loading = false;
        this.page = {
          size: size,
          currentPage: current,
          total: total,
        }
      });
    },
    serveLog(row) {
      console.log('服务日志', row)
      this.selectUserServeLog = row
      this.loadingLog = true
      getMainServeUserLog({
        idNumber: row.idNumber,
        current: this.logPage.currentPage,
        size: this.logPage.size
      }).then(res => {
        this.loadingLog = false
        const {records, current, size, total} = res.data.data
        this.logDataSource = records
        this.innerDrawerLog = true
        this.logPage = {
          size: size,
          currentPage: current,
          total: total,
        }
      })
    },
    beforeOpen(done, type) {
      if (["edit", "view", "add"].includes(type)) {
        if (type === "edit" || type === "view") {
          setTimeout(() => {
            talentsDetail({id: this.crudValues.idNumber}).then(
              (res) => {
                this.crudValues = res.data.data;
                done();
              },
              (error) => {
                window.console.log(error);
              }
            );
          }, 0);
        } else {
          done();
        }
      }
    },
    async handleSubmitLog(row, done) {
      let params = {
        ...row,
        serveId: this.selectUserServeLog.serveId,
        serveUserId: this.selectUserServeLog.serveUserId,
        talentsId: this.selectUserServeLog.id,
        idNumber: this.selectUserServeLog.idNumber,
        toName: this.selectUserServeLog.name,
        fromName: this.userInfo.real_name
      }
      console.log(params)
      let resData = await addServeLog(params)
      done()
      if (resData.data.code === 200) {
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        this.innerDrawerAddLog = false
      }
    },
  }
}

</script>

<style lang="scss" scoped>


</style>
