<template>
  <template-page
    ref="pageRef"
    :get-list="getList"
    :table-attributes="tableAttributes"
    :table-events="tableEvents"
    :options-evens-group="optionsEvensGroup"
    :moreParameters="moreParameters"
    :column-parsing="columnParsing"
    :operation="operation()"
  >
    <div class="cartographer_big">
      <el-dialog title="修改师傅数据" width="100%" :modal="false" :visible.sync="formDialog" :before-close="formCancel">
        <zj-page-container>
          <zj-page-fill class="neibuview">
            <zj-form-container ref="formRef" :form-data="formData" :form-attributes="{ size: 'mini' }">
              <zj-form-module title="基本信息" label-width="100px" :form-data="formData" :form-items="formItems" />
            </zj-form-container>
          </zj-page-fill>
          <div style="box-sizing: border-box; padding: 16px; text-align: right !important">
            <el-button @click="formCancel" size="mini">取消</el-button>
            <el-button @click="formConfirm" type="primary" size="mini">确定</el-button>
          </div>
        </zj-page-container>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import ImageUpload from '@/components/file-upload'
import { downloadFiles } from '@/utils/util'
import { required, mobileRequired, mobile, httpUrl, email } from '@/components/template/rules_verify.js'
import { listPageV2, pageExport, del, listImport, memberUserWatitUpdate } from '@/api/workerProfileInit'
import { commonTemplateDownload } from '@/api/common.js'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  components: { TemplatePage, ImageUpload },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      // 勾选选中行
      recordSelected: [],
      /** 表单变量 */
      formDialog: false,
      formData: {}
    }
  },
  computed: {
    // 事件组合
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('download', {
              click: () => {
                this.handleDownload()
              }
            })
          ],
          [
            this.optionsEvensAuth('import', {
              render: () => {
                return this.importButton(listImport, '导入师傅资料')
              }
            })
          ]
        ]
      ]
    },
    // 更多参数
    moreParameters() {
      return []
    },
    formItems() {
      return [
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '名称',
            prop: 'name',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '编号',
            prop: 'workerNumber',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '身份证',
            prop: 'idcard',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '手机号',
            prop: 'mobile',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '银行卡号',
            prop: 'bankAccount',
            rules: []
          }
        }
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList(p, cb) {
      try {
        var pam = JSON.parse(JSON.stringify(p))
        cb && cb(pam)
        return listPageV2(pam)
      } catch (error) {
        console.log(error)
      }
    },
    // 列表导出函数
    exportList: pageExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    // 表格操作列
    operation() {
      return this.operationBtn({
        del: {
          prompt: '确定删除吗？',
          click: ({ row, index, column }) => {
            del({
              id: row.id
            }).then(res => {
              if (res.code == 200) {
                this.$message({ type: 'success', message: `删除成功!` })
                this.$refs.pageRef.refreshList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        },
        edit: {
          click: ({ row, index, column }) => {
            this.formData = { ...row }
            this.$nextTick(() => {
              this.openForm()
            })
          }
        }
      })
    },
    openForm() {
      this.formDialog = true
    },
    formCancel() {
      this.$refs?.formRef?.$refs?.inlineForm?.clearValidate?.()
      this.$data.formData = this.$options.data().formData
      this.formDialog = false
    },
    formConfirm() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          memberUserWatitUpdate(this.formData).then(res => {
            this.$message({ type: 'success', message: `编辑成功!` })
            this.formCancel()
            this.$refs.pageRef.refreshList()
          })
        }
      })
    },
    handleDownload() {
      commonTemplateDownload({ name: '师傅资料初始化模板.xlsx' }, `${this.$route.meta.title}`)
        .then(res => {
          this.$message({
            message: '下载成功',
            type: 'success'
          })
        })
        .catch(err => {
          this.$message.error('下载失败')
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.neibuview {
  box-sizing: border-box;
  padding-left: 16px;

  ::v-deep & > .zj-page-fill-scroll {
    box-sizing: border-box;
    padding-right: 16px;

    & > div:nth-child(1) {
      margin-top: 20px;
    }
  }
}
</style>
