<template>
  <th-dialog
    ref="dialog"
    v-bind="dialogProps"
    :loading="loading"
  >
    <th-form
      ref="form"
      class="th-supervise--add"
      v-bind="form"
      :options="{
        orgNo: hostOrgOptions
      }"
      @handleSubmit="handleSubmit"
    />
  </th-dialog>
</template>

<script>
import baseForm from '../../../../src/mixins/baseForm'

export default {
  mixins: [baseForm],
  props: {
    request: {
      type: Object,
      default: () => ({})
    },
    hostOrgOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      loading: false,
      dialogProps: {
        title: '新建常用联系人',
        width: '500px',
        props: {
          appendToBody: true
        },
        buttons: [],
        showCancelButton: false
      },
      form: {
        labelWidth: '200px',
        data: {
          id: null,
          orgNo: '',
          realName: '',
          mobile: '',
          remark: ''
        },
        items: [
          {
            prop: 'orgNo',
            label: '所属单位',
            component: 'select',
            props: {
              filterable: true
            }
          },
          {
            prop: 'realName',
            label: '联系人名称',
            component: 'input',
            props: {
              maxlength: 200,
              placeholder: '请输入'
            }
          },
          {
            prop: 'mobile',
            label: '联系人电话',
            component: 'input',
            props: {
              maxlength: 200,
              placeholder: '请输入'
            }
          },
          {
            prop: 'remark',
            label: '备注',
            component: 'input',
            props: {
              maxlength: 200,
              placeholder: '请输入'
            }
          }
        ],
        rules: {
          orgNo: [
            this.$rules.required()
          ],
          realName: [
            this.$rules.required()
          ],
          mobile: [
            this.$rules.required()
          ]
        },
        buttons: [
          {
            key: 'Save',
            label: '保存'
          }
        ]
      }
    }
  },
  methods: {
    // 显示弹框
    showDialog(userId) {
      if (userId) {
        this.dialogProps.title = '新建常用联系人'
        this.fetchGetPerson(userId)
      } else {
        this.dialogProps.title = '修改常用联系人'
        // this.form.data = {
        //   id: null,
        //   orgNo: '',
        //   realName: '',
        //   mobile: '',
        //   remark: ''
        // }
      }
      this.$refs.dialog.showDialog()
    },
    // 获取详情
    fetchGetPerson(userId) {
      if (this.request.fetchGetPerson) {
        this.form.loading = true
        this.request.fetchGetPerson({ userId }).then(res => {
          this.form.loading = false
          if (res.data.successful && this.$length(res.data.resultValue.items)) {
            const item = res.data.resultValue.items[0]
            if (item.success) {
              const d = item.data
              this.form.data.id = d.ID
              this.form.data.orgNo = d.SYS_ORG_ID ? Number(d.SYS_ORG_ID) : ''
              this.form.data.realName = d.REAL_NAME
              this.form.data.mobile = d.MOBILE
              this.form.data.remark = d.REMARK
            }
          }
        })
      }
    },
    // 点击保存
    handleSubmit(data, btn) {
      this.$confirm('确定要保存吗？', '保存', {
        type: 'warning'
      }).then(() => {
        this.fetchSavePerson()
      })
    },
    // 保存
    fetchSavePerson() {
      if (this.request.fetchSavePerson) {
        this.form.loading = true
        this.request.fetchSavePerson(this.form.data).then(res => {
          this.form.loading = false
          if (res.data.successful) {
            this.$message.success('保存成功')
            this.$refs.dialog.handleCancel()
            this.$refs.form.resetFields()
            this.$emit('fetchTableApi')
          }
        })
      }
    }
  }
}
</script>
