<template>
  <zj-tab-page ref="tabPage" :defaultActives="[{ key: 'list', label: $route.meta.title + '-列表', essential: true }]">
    <template slot-scope="{ activeKey, data }">
      <div
        :style="{
          width: '100%',
          height: activeKey == 'list' ? '100%' : '0px',
          overflow: 'hidden'
        }"
      >
        <template-page
          ref="pageRef"
          :get-list="getList"
          :table-attributes="tableAttributes"
          :table-events="tableEvents"
          :operationColumnWidth="260"
          :options-evens-group="optionsEvensGroup"
          :moreParameters="moreParameters"
          :column-parsing="columnParsing"
          :operation="operation()"
          :exportList="exportList"
        >
          <div slot="moreSearch">
            <el-radio-group v-model="examineStatus" size="mini" @change="changeType">
              <el-radio-button label="">全部</el-radio-button>
              <el-radio-button label="WAIT">入驻申请待审核({{ workerManagerCount.rzsp }})</el-radio-button>
              <el-radio-button label="WAIT_WORKER">保险待购买({{ workerManagerCount.dsfgm }})</el-radio-button>
              <el-radio-button label="POLICY_WAIT">保险待审核({{ workerManagerCount.bxsp }})</el-radio-button>
              <el-radio-button label="OK">状态正常({{ workerManagerCount.ztzc }})</el-radio-button>
              <el-radio-button label="FAIL">已驳回({{ workerManagerCount.ybh }})</el-radio-button>
              <el-radio-button label="EXPIRED">证件快过期({{ workerManagerCount.zjkgq }})</el-radio-button>
              <el-radio-button label="OVERDUE">证件已过期({{ workerManagerCount.zjygq }})</el-radio-button>
              <el-radio-button label="STOP">已暂停({{ workerManagerCount.yzt }})</el-radio-button>
              <el-radio-button label="QUIT">已经离职({{ workerManagerCount.ylz }})</el-radio-button>
            </el-radio-group>
            <br /><br />
          </div>
        </template-page>
      </div>
      <div
        v-if="~['detail', 'edit', 'entryAudit', 'insuranceAudit'].indexOf(activeKey)"
        style="box-sizing: border-box; padding: 16px"
      >
        <zj-form-container ref="formRef" :form-data="formData">
          <zj-form-module
            title="详情"
            label-width="100px"
            :showPackUp="false"
            :form-data="formData.user"
            :form-items="formItems"
          >
          </zj-form-module>
          <zj-form-module
            title="证件信息"
            label-width="100px"
            :showPackUp="false"
            :form-data="formData"
            :form-items="formItems2"
          >
          </zj-form-module>
          <zj-form-module
            title="保险信息"
            label-width="100px"
            :showPackUp="false"
            :form-data="formData"
            :form-items="formItems3"
          >
          </zj-form-module>
          <zj-form-module
            title="操作记录"
            label-width="100px"
            :showPackUp="false"
            :form-data="formData"
            :form-items="formItems4"
          >
          </zj-form-module>
        </zj-form-container>
        <div slot="footer" class="dialog-footer">
          <el-button
            v-if="[2, 3].includes(openType)"
            size="mini"
            type="primary"
            @click="
              () => {
                if ([2].includes(openType)) {
                  formData.examineStatus = 'WAIT_WORKER'
                } else if ([3].includes(openType)) {
                  formData.examineStatus = 'OK'
                }
                shenheBool = true
              }
            "
            >审核通过</el-button
          >
          <el-button
            v-if="[2, 3].includes(openType)"
            size="mini"
            type="danger"
            @click="
              () => {
                formData.examineStatus = 'FAIL'
                shenheBool = true
              }
            "
            >审核驳回</el-button
          >
          <el-button v-if="activeKey == 'edit'" size="mini" type="primary" @click="submit(data.removeTab)"
            >保存</el-button
          >
          <el-button size="mini" @click="data.removeTab()">取 消</el-button>
        </div>
        <el-dialog title="审核" width="700px" :modal="false" :visible.sync="shenheBool" :before-close="shenheClose">
          <el-form ref="form" :model="formData" label-width="120px" size="mini">
            <el-form-item label="入驻网点">
              <span>{{ `(${formData.websitId})${formData.websitName}` }}</span>
            </el-form-item>
            <el-form-item label="师傅名称">
              <span>{{ formData.user && formData.user.nickName }}</span>
            </el-form-item>
            <el-form-item label="联系电话">
              <span>{{ formData.user && formData.user.mobile }}</span>
            </el-form-item>
            <el-form-item label="师傅编号">
              <span>{{ formData.user && formData.user.workerNumber }}</span>
            </el-form-item>
            <el-form-item label="审核备注" v-if="[2].includes(openType)">
              <el-input type="textarea" v-model="formData.examineRemark"></el-input>
            </el-form-item>
            <el-form-item label="审核备注" v-if="[3].includes(openType)">
              <el-input type="textarea" v-model="formData.policyExamineRemark"></el-input>
            </el-form-item>
            <el-form-item>
              <div style="text-align: right">
                <el-button @click="shenheClose">取消</el-button>
                <el-button type="primary" v-if="[2].includes(openType)" @click="examineWait(data.removeTab)"
                  >确定</el-button
                >
                <el-button type="primary" v-if="[3].includes(openType)" @click="workerManagerExamine(data.removeTab)"
                  >确定</el-button
                >
              </div>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
      <div v-if="~['masterGroup'].indexOf(activeKey)" style="box-sizing: border-box; padding: 16px">
        <zj-table :columns="evaluationColumns" :table-data="evaluationData" />
      </div>
    </template>
  </zj-tab-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import { required, mobileRequired, mobile } from '@/components/template/rules_verify.js'
import {
  memberListPageV2,
  memberPageExport,
  workerManagerDetail,
  workerManagerActivation,
  workerManagerAgainQuit,
  workerManagerQuit,
  workerManagerStop,
  workerManagerUpdate,
  workerManagerExamineWait,
  workerManagerExamine,
  workerManagerCount,
  workerManagerListLog
} from '@/api/masterManagement'
import operation_mixin from '@/components/template/operation_mixin.js'
import ImageUpload from '@/components/file-upload'
import editTable from '@/components/template/editTable.js'
import { tableDataParsing } from '@/utils/common.js'
import { workerTeamList } from '@/api/masterGroup.js'

export default {
  components: { TemplatePage, ImageUpload },
  mixins: [import_mixin, operation_mixin, editTable],
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false,
        'row-class-name': this.tableRowClassName
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      // 勾选选中行
      recordSelected: [],
      /** 表单变量 */
      formData: {
        bankAccount: '',
        companyWechatId: '',
        companyWechatName: '',
        createTime: '',
        examineBy: '',
        examineRemark: '',
        examineStatus: '',
        examineTime: '',
        slaveWorkerId: '',
        userId: '',
        websitId: '',
        websitName: '',
        policyExamineRemark: '',
        workerNumber: '',
        workerImgs: [],
        user: {},
        workerImgsCp: [{ type: '', number: '', startTime: '', endTime: '', reexamineTime: '', filesList: [] }],
        workerPolicies: [],
        nickName: '',
        mobile: ''
      },
      examineStatus: '',
      openType: 0,
      shenheBool: false,
      evaluationColumns: [],
      evaluationData: [],
      workerManagerCount: {},
      workerManagerListLog: []
    }
  },
  computed: {
    // 更多参数
    moreParameters() {
      return []
    },
    formItems() {
      return [
        {
          md: 6,
          isShow: true,
          name: 'el-select',
          options: [
            { label: '入住申请待审核', value: 'WAIT' },
            { label: '待师傅购买', value: 'WAIT_WORKER' },
            { label: '保险待审核', value: 'POLICY_WAIT' },
            { label: '状态正常', value: 'OK' },
            { label: '已驳回', value: 'FAIL' },
            { label: '证件快过期', value: 'EXPIRED' },
            { label: '证件已过期', value: 'OVERDUE' },
            { label: '已暂停', value: 'STOP' },
            { label: '已经离职', value: 'QUIT' }
          ],
          attributes: { placeholder: '请输入', disabled: true },
          formItemAttributes: {
            label: '状态',
            prop: 'examineStatus_t',
            rules: []
          }
        },
        {
          md: 6,
          isShow: true,
          name: 'el-select',
          options: [
            { label: '普通用户', value: 'GENERAL' },
            { label: '分销员', value: 'SERVICE' },
            { label: '师傅', value: 'WORKER' }
          ],
          attributes: { placeholder: '请输入', disabled: true },
          formItemAttributes: {
            label: '用户类型',
            prop: 'type',
            rules: []
          }
        },
        {
          md: 6,
          isShow: true,
          name: 'el-input',
          attributes: { placeholder: '请输入' },
          formItemAttributes: {
            label: '师傅名称',
            prop: 'nickName',
            rules: [...required]
          }
        },
        {
          md: 6,
          isShow: true,
          name: 'el-input',
          attributes: { placeholder: '请输入' },
          formItemAttributes: {
            label: '联系电话',
            prop: 'mobile',
            rules: [...required]
          }
        },
        {
          name: 'slot-component',
          md: 12,
          isShow: true,
          attributes: { placeholder: '请输入', disabled: true },
          formItemAttributes: {
            label: '入驻网点',
            prop: 'websitName',
            rules: [...required]
          },
          render: (h, { props, onInput }) => {
            return (
              <el-input
                value={`(${this.formData.websitId || ''})${this.formData.websitName || ''}`}
                disabled={true}
                size="mini"
                placeholder="请输入"
              ></el-input>
            )
          }
        },
        {
          md: 6,
          isShow: true,
          name: 'el-input',
          attributes: { placeholder: '请输入' },
          formItemAttributes: {
            label: '师傅编号',
            prop: 'workerNumber',
            rules: []
          }
        },
        {
          md: 6,
          isShow: true,
          name: 'el-input',
          attributes: { placeholder: '请输入' },
          formItemAttributes: {
            label: '银行卡',
            prop: 'bankAccount',
            rules: []
          }
        },
        {
          md: 24,
          isShow: true,
          name: 'el-input',
          attributes: {
            placeholder: '请输入',
            type: 'textarea'
          },
          formItemAttributes: {
            label: '备注',
            prop: 'examineRemark',
            rules: []
          }
        }
      ]
    },
    formItems2() {
      return [
        {
          name: 'slot-component',
          md: 24,
          formItemAttributes: {
            'label-width': '0px',
            label: '',
            prop: 'items',
            rules: []
          },
          render: (h, { props }) => {
            return (
              <zj-table
                columns={[
                  {
                    columnAttributes: {
                      label: '证件名称',
                      prop: 'type'
                    },
                    render: (h, { row, column, index }) => {
                      return (
                        <div class="redbordererr">
                          <el-form-item
                            label=""
                            label-width="0px"
                            prop={`workerImgsCp.${index}.${column.columnAttributes.prop}`}
                          >
                            <el-select
                              value={row[column.columnAttributes.prop]}
                              onInput={val => {
                                row[column.columnAttributes.prop] = val
                              }}
                              placeholder="请选择"
                              disabled={true}
                            >
                              {[
                                { value: 'HIGHT', label: '高空证' },
                                { value: 'ID', label: '身份证' }
                              ].map((item, index_) => (
                                <el-option key={index_} label={item.label} value={item.value}></el-option>
                              ))}
                            </el-select>
                          </el-form-item>
                        </div>
                      )
                    }
                  },
                  {
                    columnAttributes: {
                      label: '证件号码',
                      prop: 'number'
                    },
                    render: (h, { row, column, index }) => {
                      return (
                        <div class="redbordererr">
                          <el-input
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                          ></el-input>
                        </div>
                      )
                    }
                  },
                  {
                    columnAttributes: {
                      label: '有效期开始日期',
                      prop: 'startTime'
                    },
                    render: (h, { row, column, index }) => {
                      return (
                        <div class="redbordererr">
                          <el-form-item
                            label=""
                            label-width="0px"
                            prop={`workerImgsCp.${index}.${column.columnAttributes.prop}`}
                          >
                            <el-date-picker
                              value={row[column.columnAttributes.prop]}
                              value-format={'yyyy-MM-dd HH:mm:ss'}
                              onInput={val => {
                                row[column.columnAttributes.prop] = val
                              }}
                              type="date"
                              placeholder="选择日期"
                            ></el-date-picker>
                          </el-form-item>
                        </div>
                      )
                    }
                  },
                  {
                    columnAttributes: {
                      label: '有效期结束日期',
                      prop: 'endTime'
                    },
                    render: (h, { row, column, index }) => {
                      return (
                        <div class="redbordererr">
                          <el-form-item
                            label=""
                            label-width="0px"
                            prop={`workerImgsCp.${index}.${column.columnAttributes.prop}`}
                          >
                            <el-date-picker
                              value={row[column.columnAttributes.prop]}
                              onInput={val => {
                                row[column.columnAttributes.prop] = val
                              }}
                              value-format={'yyyy-MM-dd HH:mm:ss'}
                              type="date"
                              placeholder="选择日期"
                            ></el-date-picker>
                          </el-form-item>
                        </div>
                      )
                    }
                  },
                  {
                    columnAttributes: {
                      label: '复审日期',
                      prop: 'reexamineTime'
                    },
                    render: (h, { row, column, index }) => {
                      if (index) {
                        return (
                          <div class="redbordererr">
                            <el-form-item
                              label=""
                              label-width="0px"
                              prop={`workerImgsCp.${index}.${column.columnAttributes.prop}`}
                            >
                              <el-date-picker
                                value={row[column.columnAttributes.prop]}
                                onInput={val => {
                                  row[column.columnAttributes.prop] = val
                                }}
                                value-format={'yyyy-MM-dd HH:mm:ss'}
                                type="date"
                                placeholder="选择日期"
                              ></el-date-picker>
                            </el-form-item>
                          </div>
                        )
                      } else {
                        return null
                      }
                    }
                  },
                  {
                    columnAttributes: {
                      label: '正面',
                      prop: 'positive',
                      width: 130
                    },
                    render: (h, { row, column, index }) => {
                      return (
                        <div class="redbordererr">
                          <ImageUpload fileList={row[column.columnAttributes.prop]} limit={1} isEdit={true} />
                        </div>
                      )
                    }
                  },
                  {
                    columnAttributes: {
                      label: '反面',
                      prop: 'negative',
                      width: 130
                    },
                    render: (h, { row, column, index }) => {
                      return (
                        <div class="redbordererr">
                          <ImageUpload fileList={row[column.columnAttributes.prop]} limit={1} isEdit={true} />
                        </div>
                      )
                    }
                  }
                ]}
                table-data={this.formData.workerImgsCp || []}
              />
            )
          }
        }
      ]
    },
    formItems3() {
      return [
        {
          name: 'slot-component',
          md: 24,
          formItemAttributes: {
            label: '',
            'label-width': '0px',
            prop: 'workerPolicies',
            rules: []
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return this.convertTableJson(
              value,
              [
                {
                  columnAttributes: {
                    label: '保险公司名字',
                    prop: 'company',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                          rules={
                            row.type == 'IN' || ~[1, 2, 3].indexOf(this.openType)
                              ? []
                              : [{ required: true, message: '请填写', trigger: 'blur' }]
                          }
                        >
                          <el-input
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                            disabled={row.type == 'IN' || !~[1, 2, 3].indexOf(this.openType)}
                          ></el-input>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '购买类型',
                    prop: 'type',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-select
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                            disabled={true}
                          >
                            {[
                              { label: '线下购买', value: 'LINE' },
                              { label: '在线购买', value: 'IN' }
                            ].map((item, index_) => (
                              <el-option key={index_} label={item.label} value={item.value}></el-option>
                            ))}
                          </el-select>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '保险单名称',
                    prop: 'policyName',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-input
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                            disabled={row.type == 'IN'}
                          ></el-input>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '保单类型',
                    prop: 'policyType',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-select
                            disabled={row.type == 'IN'}
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                          >
                            {[
                              { label: '意外险', value: 'AC' },
                              { label: '雇主险', value: 'EM' },
                              { label: '工伤险', value: 'IN' }
                            ].map((item, index_) => (
                              <el-option key={index_} label={item.label} value={item.value}></el-option>
                            ))}
                          </el-select>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '保单状态',
                    prop: 'status',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-select
                            disabled={true}
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                          >
                            {[
                              { label: '保障中', value: 'BZZ' },
                              { label: '失效', value: 'YSX' },
                              { label: '待生效', value: 'DSX' }
                            ].map((item, index_) => (
                              <el-option key={index_} label={item.label} value={item.value}></el-option>
                            ))}
                          </el-select>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '保险单号',
                    prop: 'policyOrder',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                        >
                          {/* rules={[{ required: true, message: '请填写', trigger: 'blur' }]} */}
                          <el-input
                            disabled={row.type == 'IN'}
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            placeholder="请输入内容"
                          ></el-input>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '有效期开始日期',
                    prop: 'startTime',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-date-picker
                            disabled={row.type == 'IN'}
                            value-format="yyyy-MM-dd HH:mm:ss"
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            type="date"
                            placeholder="选择日期"
                          ></el-date-picker>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '有效期结束日期',
                    prop: 'endTime',
                    'min-width': '200px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <el-form-item
                          label=""
                          label-width="0px"
                          prop={`workerPolicies.${index}.${column.columnAttributes.prop}`}
                          rules={[{ required: true, message: '请填写', trigger: 'blur' }]}
                        >
                          <el-date-picker
                            disabled={row.type == 'IN'}
                            value-format="yyyy-MM-dd HH:mm:ss"
                            value={row[column.columnAttributes.prop]}
                            onInput={val => {
                              row[column.columnAttributes.prop] = val
                            }}
                            type="date"
                            placeholder="选择日期"
                          ></el-date-picker>
                        </el-form-item>
                      </div>
                    )
                  }
                },
                {
                  columnAttributes: {
                    label: '附件',
                    prop: 'policyUrls',
                    'min-width': '440px'
                  },
                  render: (h, { row, column, index }) => {
                    return (
                      <div class="redbordererr">
                        <ImageUpload
                          fileList={row[column.columnAttributes.prop]}
                          limit={100}
                          isEdit={row.type !== 'IN'}
                        />
                      </div>
                    )
                  }
                }
              ],
              {
                isEdit: true,
                isAdd: this.openType !== 0 && value.filter(item => item.status === 'BZZ').length < 2,
                isDel: true,
                isUpdate: false,
                isConfirm: false
              },
              {
                add: () => {
                  this.formData.workerPolicies.push({
                    company: '',
                    endTime: '',
                    exTime: '',
                    policyId: '',
                    policyName: '',
                    policyOrder: '',
                    policyPrice: '',
                    policyType: '',
                    startTime: '',
                    status: '',
                    type: 'LINE',
                    websitId: '',
                    websitName: '',
                    websitUserId: '',
                    workerId: '',
                    workerName: ''
                  })
                  this.isEditTableIndex = this.formData.workerPolicies.length - 1
                },
                delete: ({ row, column, index }, cb) => {
                  if (this.isEditTableIndex == index) {
                    this.isEditTableIndex = -1
                  } else if (this.isEditTableIndex > index) {
                    this.isEditTableIndex--
                  }
                  cb && cb()
                }
              }
            )
          }
        }
      ]
    },
    formItems4() {
      return [
        {
          name: 'slot-component',
          md: 24,
          formItemAttributes: {
            'label-width': '0px',
            label: '',
            prop: '-',
            rules: []
          },
          render: (h, { props }) => {
            return (
              <zj-table
                columns={[
                  {
                    columnAttributes: {
                      label: '操作类型',
                      prop: 'typeText'
                    }
                  },
                  {
                    columnAttributes: {
                      label: '操作内容',
                      prop: 'opRemark'
                    }
                  },
                  {
                    columnAttributes: {
                      label: '操作人',
                      prop: 'createBy'
                    }
                  },
                  {
                    columnAttributes: {
                      label: '操作时间',
                      prop: 'createTime'
                    }
                  }
                ]}
                table-data={this.workerManagerListLog}
              />
            )
          }
        }
      ]
    }
  },
  watch: {
    'formData.user': {
      handler(val) {
        this.formData.nickName = this.formData?.user?.nickName
        this.formData.mobile = this.formData?.user?.mobile
        this.formData.examineRemark = this.formData?.user?.examineRemarkCP
      },
      deep: true
    }
  },
  methods: {
    // 切换状态
    changeType(val) {
      this.$refs.pageRef.refreshList()
    },
    exportList: memberPageExport,
    // 列表请求函数
    getList(p) {
      try {
        var pam = JSON.parse(JSON.stringify(p))
        if (this.examineStatus === 'EXPIRED') {
          pam.params.push({ param: 'b.is_overdue', compare: '=', value: 'YES' })
        } else {
          pam.params.push({ param: 'b.examine_status', compare: '=', value: this.examineStatus })
        }
        return memberListPageV2(pam)
      } catch (error) {
        console.log(error)
      } finally {
        workerManagerCount().then(res => {
          this.workerManagerCount = res.data
        })
      }
    },
    // 列表导出函数
    exportList: memberPageExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      if (item.jname === 'idCardImg') {
        defaultData.render = (h, { row, index, column }) => {
          return (
            <div style="padding:0 6px;cursor: pointer;">
              {row.idCardImg
                ? row.idCardImg
                    .split(',')
                    .map(url => (
                      <el-image
                        src={this.$showImgUrl(url)}
                        preview-src-list={[this.$showImgUrl(url)]}
                        fit="fit"
                        style="width:80px;height:80px;"
                      />
                    ))
                : null}
            </div>
          )
        }
      }
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    // 表格操作列
    operation() {
      return this.operationBtn({
        // 详情
        detail: {
          btnType: 'text',
          click: ({ row, index, column }) => {
            this.$refs.tabPage.addTab({
              activeKey: 'detail',
              key: 'detail',
              label: '详情',
              triggerEvent: () => {
                this.guanbi()
                this.openType = 0
                this.$nextTick(() => {
                  this.openWindows(row)
                })
              },
              closeEvent: () => {}
            })
          }
        },
        // 编辑
        edit: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return (
              ['OK', 'FAIL', 'STOP', 'QUIT', 'OVERDUE'].includes(row.examineStatus) ||
              (['MAJOR', 'MERCHANT'].includes(row.attr) && ['WAIT_WORKER'].includes(row.examineStatus))
            )
          },
          click: ({ row, index, column }) => {
            this.$refs.tabPage.addTab({
              activeKey: 'edit',
              key: 'edit',
              label: '编辑',
              triggerEvent: () => {
                this.guanbi()
                this.openType = 1
                this.$nextTick(() => {
                  this.openWindows(row)
                })
              },
              closeEvent: () => {}
            })
          }
        },
        // 入驻申请审核
        entryAudit: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return ['WAIT'].includes(row.examineStatus)
          },
          click: ({ row, index, column }) => {
            this.$refs.tabPage.addTab({
              activeKey: 'entryAudit',
              key: 'entryAudit',
              label: '入驻申请审核',
              triggerEvent: () => {
                this.guanbi()
                this.openType = 2
                this.$nextTick(() => {
                  this.openWindows(row)
                })
              },
              closeEvent: () => {}
            })
          }
        },
        // 保险审核
        insuranceAudit: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return ['POLICY_WAIT'].includes(row.examineStatus)
          },
          click: ({ row, index, column }) => {
            this.$refs.tabPage.addTab({
              activeKey: 'insuranceAudit',
              key: 'insuranceAudit',
              label: '保险审核',
              triggerEvent: () => {
                this.guanbi()
                this.openType = 3
                this.$nextTick(() => {
                  this.openWindows(row)
                })
              },
              closeEvent: () => {}
            })
          }
        },
        // 激活
        activate: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return ['STOP'].includes(row.examineStatus)
          },
          click: ({ row, index, column }) => {
            workerManagerActivation({ id: row.id }).then(res => {
              this.$message({ type: 'success', message: '激活成功!' })
              this.$refs.pageRef.refreshList()
            })
          }
        },
        // 重新入职
        reEntry: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return ['QUIT'].includes(row.examineStatus)
          },
          click: ({ row, index, column }) => {
            workerManagerAgainQuit({ id: row.id }).then(res => {
              this.$message({ type: 'success', message: '入职成功!' })
              this.$refs.pageRef.refreshList()
            })
          }
        },
        // 暂停
        pause: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return ['OK'].includes(row.examineStatus)
          },
          click: ({ row, index, column }) => {
            workerManagerStop({ id: row.id }).then(res => {
              this.$message({ type: 'success', message: '暂停成功!' })
              this.$refs.pageRef.refreshList()
            })
          }
        },
        // 离职
        dimission: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return ['OK'].includes(row.examineStatus)
          },
          click: ({ row, index, column }) => {
            workerManagerQuit({ id: row.id }).then(res => {
              this.$message({ type: 'success', message: '离职成功!' })
              this.$refs.pageRef.refreshList()
            })
          }
        },
        // 师傅组别
        masterGroup: {
          btnType: 'text',
          conditions: ({ row, index, column }) => {
            return true
          },
          click: ({ row, index, column }) => {
            this.evaluationData = []
            this.$refs.tabPage.addTab({
              activeKey: 'masterGroup',
              key: 'masterGroup',
              label: '师傅组别',
              triggerEvent: () => {
                workerTeamList({
                  pageNum: 1,
                  pageSize: -1,
                  params: [{ param: 'a.websit_id', compare: 'like', value: row.websitId }],
                  workerId: row.userId
                }).then(res => {
                  this.evaluationColumns = tableDataParsing(
                    res.fieldBeans.filter(item => !~['orderFlags', 'orderType'].indexOf(item.jname))
                  ).map(item => {
                    return item
                  })
                  this.$nextTick(() => {
                    this.evaluationData = res.data.records.map(item => {
                      Object.keys(item).map(key => {
                        var val = JSON.parse(res?.fieldBeans?.find(val => val.jname == key)?.enumMap || '{}')[item[key]]
                        if (val) item[key] = val
                      })
                      return item
                    })
                  })
                })
              },
              closeEvent: () => {}
            })
          }
        }
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.examineStatus === '待审核') {
        return 'redbackground'
      } else {
        return ''
      }
    },
    guanbi() {
      this.$refs?.formRef?.$refs?.inlineForm?.clearValidate()
      this.$data.formData = this.$options.data().formData
    },
    openWindows(row, cb) {
      workerManagerListLog({ id: row.id }).then(res => {
        this.workerManagerListLog = res.data
      })
      workerManagerDetail({ id: row.id }).then(res => {
        this.formData = {
          ...res.data,
          user: {
            ...res.data.user,
            examineStatus_t: res.data.examineStatus,
            websitName: res.data.websitName,
            examineRemarkCP: res.data.examineRemark
          },
          workerImgsCp: [
            {
              type: 'ID',
              number: res.data?.user?.idCard || '',
              startTime: res.data?.user?.idCardStartTime || '',
              endTime: res.data?.user?.idCardEndTime || '',
              reexamineTime: '',
              positive: (() => {
                var data = (res.data?.workerImgs || [])
                  .filter(item => item.type == 'ID')
                  .map(item => ({
                    name: item.imgName,
                    url: item.imgUrl
                  }))?.[0]

                if (data) {
                  return [data]
                }
                return []
              })(),
              negative: (() => {
                var data = (res.data?.workerImgs || [])
                  .filter(item => item.type == 'ID')
                  .map(item => ({
                    name: item.imgName,
                    url: item.imgUrl
                  }))?.[1]

                if (data) {
                  return [data]
                }
                return []
              })()
            },
            {
              type: 'HIGHT',
              number: res.data?.user?.number || '',
              startTime: res.data?.user?.startTime || '',
              endTime: res.data?.user?.endTime || '',
              reexamineTime: res.data?.user?.hightExamineTime || '',
              positive: (() => {
                var data = (res.data?.workerImgs || [])
                  .filter(item => item.type == 'HIGHT')
                  .map(item => ({
                    name: item.imgName,
                    url: item.imgUrl
                  }))?.[0]

                if (data) {
                  return [data]
                }
                return []
              })(),
              negative: (() => {
                var data = (res.data?.workerImgs || [])
                  .filter(item => item.type == 'HIGHT')
                  .map(item => ({
                    name: item.imgName,
                    url: item.imgUrl
                  }))?.[1]

                if (data) {
                  return [data]
                }
                return []
              })()
            }
          ],
          workerPolicies: res.data?.workerPolicies?.map(item => {
            return {
              ...item,
              policyUrls: item.policyUrl ? item.policyUrl.split(',').map(url => ({ url })) : []
            }
          })
        }
        cb?.()
        console.log(this.formData)
      })
    },
    getPam() {
      return {
        ...this.formData,
        user: {
          ...this.formData.user,
          idCard: this.formData?.workerImgsCp?.find(item => item.type == 'ID')?.number,
          idCardStartTime: this.formData?.workerImgsCp?.find(item => item.type == 'ID')?.startTime,
          idCardEndTime: this.formData?.workerImgsCp?.find(item => item.type == 'ID')?.endTime,
          number: this.formData?.workerImgsCp?.find(item => item.type == 'HIGHT')?.number,
          startTime: this.formData?.workerImgsCp?.find(item => item.type == 'HIGHT')?.startTime,
          endTime: this.formData?.workerImgsCp?.find(item => item.type == 'HIGHT')?.endTime,
          hightExamineTime: this.formData?.workerImgsCp?.find(item => item.type == 'HIGHT')?.reexamineTime
        },
        workerImgs: [
          ...(() => {
            var url = this.formData?.workerImgsCp?.find(item => item.type == 'ID')?.positive?.[0]?.url
            if (url) {
              return [
                {
                  type: 'ID',
                  imgName: '身份证正面',
                  imgUrl: url
                }
              ]
            } else {
              return []
            }
          })(),
          ...(() => {
            var url = this.formData?.workerImgsCp?.find(item => item.type == 'ID')?.negative?.[0]?.url
            if (url) {
              return [
                {
                  type: 'ID',
                  imgName: '身份证反面',
                  imgUrl: url
                }
              ]
            } else {
              return []
            }
          })(),
          ...(() => {
            var url = this.formData?.workerImgsCp?.find(item => item.type == 'HIGHT')?.positive?.[0]?.url
            if (url) {
              return [
                {
                  type: 'HIGHT',
                  imgName: '高空证正面',
                  imgUrl: url
                }
              ]
            } else {
              return []
            }
          })(),
          ...(() => {
            var url = this.formData?.workerImgsCp?.find(item => item.type == 'HIGHT')?.negative?.[0]?.url
            if (url) {
              return [
                {
                  type: 'HIGHT',
                  imgName: '高空证反面',
                  imgUrl: url
                }
              ]
            } else {
              return []
            }
          })()
        ],
        workerPolicies: this.formData?.workerPolicies?.map(item => {
          return {
            ...item,
            policyUrl: item.policyUrls?.map(v => v.url)?.join(',')
          }
        })
      }
    },
    submit(Close_) {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          var data = this.getPam()
          if (
            data?.user?.startTime &&
            data?.user?.endTime &&
            data.user.hightExamineTime &&
            (new Date(data.user.hightExamineTime) < new Date(data.user.startTime) ||
              new Date(data.user.hightExamineTime) > new Date(data.user.endTime))
          ) {
            this.$message({ type: 'warning', message: `复审日期必须必须在有效期内!` })
            return
          }

          if (this.formData.user.workerNumber) {
            this.shenheClose()
            workerManagerUpdate(this.getPam()).then(res => {
              this.$message({ type: 'success', message: `保存成功!` })
              this.guanbi()
              this.$refs.pageRef.refreshList()
              Close_?.()
            })
          } else {
            this.$confirm('师傅编号为空, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
              .then(() => {
                this.shenheClose()
                workerManagerUpdate(this.getPam()).then(res => {
                  this.$message({ type: 'success', message: `保存成功!` })
                  this.guanbi()
                  this.$refs.pageRef.refreshList()
                  Close_?.()
                })
              })
              .catch(() => {})
          }
        }
      })
    },
    examineWait(Close_) {
      var data = this.getPam()
      if (
        data?.user?.startTime &&
        data?.user?.endTime &&
        data.user.hightExamineTime &&
        (new Date(data.user.hightExamineTime) < new Date(data.user.startTime) ||
          new Date(data.user.hightExamineTime) > new Date(data.user.endTime))
      ) {
        this.$message({ type: 'warning', message: `复审日期必须必须在有效期内!` })
        return
      }
      this.shenheClose()
      workerManagerExamineWait(this.getPam()).then(res => {
        this.$message({ type: 'success', message: `操作成功!` })
        this.$refs.pageRef.refreshList()
        Close_?.()
      })
    },
    workerManagerExamine(Close_) {
      var data = this.getPam()
      if (
        data?.user?.startTime &&
        data?.user?.endTime &&
        data.user.hightExamineTime &&
        (new Date(data.user.hightExamineTime) < new Date(data.user.startTime) ||
          new Date(data.user.hightExamineTime) > new Date(data.user.endTime))
      ) {
        this.$message({ type: 'warning', message: `复审日期必须必须在有效期内!` })
        return
      }
      this.shenheClose()
      workerManagerExamine(this.getPam()).then(res => {
        this.$message({ type: 'success', message: `操作成功!` })
        this.$refs.pageRef.refreshList()
        Close_?.()
      })
    },
    shenheClose() {
      this.shenheBool = false
    }
  }
}
</script>

<style lang="scss" scoped>
.tab {
  padding: 20px 20px 0 20px;
}
</style>
<style lang="scss">
.redbackground {
  background: rgb(248, 185, 185) !important;
}
.lcsebackground {
  background: rgb(212, 245, 212) !important;
}
.dialog-footer {
  text-align: right !important;
}
</style>
