<template>
  <th-dialog
    ref="dialog"
    class="th-arrang-time"
    v-bind="dialogProps"
    :loading="loading"
    @handleConfirm="handleConfirm"
  >
    <th-form
      ref="form"
      v-bind="form"
      :options="{
        orgNo: hostOrgOptions,
        sheetType: sheetTypeOptions
      }"
      @handleSubmit="handleSubmit"
    />
  </th-dialog>
</template>

<script>
import ThDialog from '../../dialog/src/main'
import ThForm from '../../form/src/form'
import baseForm from '../../../src/mixins/baseForm'
import reqMixin from './reqMixin'

export default {
  name: 'ThArrangTime',
  components: {
    ThDialog,
    ThForm
  },
  mixins: [baseForm, reqMixin],
  data() {
    const style = {
      display: 'inline-block',
      width: '33%'
    }
    return {
      loading: false,
      dialogProps: {
        title: '新建约时工单',
        width: '80%',
        props: {
          appendToBody: true
        },
        buttons: [
          {
            key: 'submit',
            label: '提交'
          }
        ]
      },
      form: {
        labelWidth: '120px',
        data: {
          appNo: '',
          sheetType: '1',
          relAppNo: '',
          customerName: '',
          customerNo: '',
          customerPhone: '',
          orgNo: '',
          arrangeCompleteTime: '',
          arrangPerson: '',
          customerAddress: '',
          handleContent: ''
        },
        items: [
          {
            label: '工单信息',
            component: 'title',
            props: {
              title: '工单信息',
              type: 'primary'
            }
          },
          {
            label: '工单编号',
            prop: 'appNo',
            component: 'input-button',
            buttonText: '新',
            buttonClick: this.newAppNo,
            style
          },
          {
            label: '业务类型',
            prop: 'sheetType',
            component: 'select',
            style
          },
          {
            label: '关联工单',
            prop: 'relAppNo',
            component: 'input',
            style,
            props: {
              maxlength: 200,
              disabled: true
            }
          },
          {
            label: '客户名称',
            prop: 'customerName',
            component: 'input',
            props: {
              placeholder: '请输入',
              maxlength: 64
            },
            style
          },
          {
            label: '客户编号',
            prop: 'customerNo',
            component: 'input',
            props: {
              placeholder: '请输入',
              maxlength: 64
            },
            style
          },
          {
            label: '联系电话',
            prop: 'customerPhone',
            component: 'input',
            props: {
              placeholder: '请输入',
              maxlength: 64
            },
            style
          },
          {
            label: '工单单位',
            prop: 'orgNo',
            component: 'select',
            style
          },
          {
            label: '约定完成时间',
            prop: 'arrangeCompleteTime',
            component: 'date',
            props: {

            },
            style
          },
          {
            label: '约定人',
            prop: 'arrangPerson',
            component: 'input',
            props: {
              placeholder: '请输入',
              maxlength: 64
            },
            style
          },
          {
            label: '用户地址',
            prop: 'customerAddress',
            component: 'input',
            props: {
              type: 'textarea',
              autosize: {
                minRows: 1
              },
              placeholder: '请输入',
              maxlength: 1024
            }
          },
          {
            label: '约定内容',
            prop: 'handleContent',
            component: 'input',
            props: {
              type: 'textarea',
              autosize: {
                minRows: 4
              },
              maxlength: 500
            }
          }
        ],
        rules: {
          appNo: [
            this.$rules.required()
          ],
          sheetType: [
            this.$rules.required()
          ],
          customerPhone: [
            this.$rules.required(),
            this.$rules.phoneAll()
          ],
          orgNo: [
            this.$rules.required()
          ],
          arrangeCompleteTime: [
            this.$rules.required()
          ],
          arrangPerson: [
            this.$rules.required()
          ],
          customerAddress: [
            this.$rules.required()
          ],
          handleContent: [
            this.$rules.required()
          ]
        },
        buttons: []
      }
    }
  },
  watch: {
    'form.data.sheetType': {
      handler(val) {
        if (val === '9') {
          this.form.data.relAppNo = ''
        }
        if (val === '14' && this.pageType === 'localRequest') {
          this.$message.info('诉求类工单发起约时，请到对应处理页面中发起！')
          this.form.data.sheetType = ''
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.FetchHostOrgOptions()
    this.FetchSheetTypeOptions()
  },
  methods: {
    // 显示弹框
    showDialog(data) {
      if (data) {
        this.form.data.relAppNo = data.appNo || ''
        // this.form.data.sheetType = data.sheettype || ''
        this.form.data.customerName = data.custName || ''
        this.form.data.customerNo = data.custNo || ''
        this.form.data.customerAddress = data.faultAddress || ''
        this.form.data.customerPhone = data.tel || ''
        this.form.data.orgNo = data.sysOrgId || ''
        this.newAppNo()
      }
      this.$refs.dialog.showDialog()
    },
    // 生成新的工单编号
    newAppNo() {
      this.form.data.appNo = new Date().getTime()
    },
    // 点击确定
    handleConfirm() {
      this.$refs.form.handleSubmit({})
    },
    // 表单提交
    handleSubmit(data, btn) {
      this.$confirm('确定提交吗？', '提交', {
        type: 'warning'
      }).then(() => {
        this.fetchCreateArrangeTime(data)
        // this.fetchGetDisptachSheet()
      })
    },
    // 新建约时工单
    fetchCreateArrangeTime(data) {
      if (this.request.fetchCreateArrangeTime) {
        this.loading = true
        this.request.fetchCreateArrangeTime({ model: data }).then(res => {
          this.loading = false
          if (res.data.code === 0) {
            this.$message.success('操作成功！')
            this.$refs.form.resetFields()
            this.$refs.dialog.handleCancel()
          } else {
            this.$message.warning(res.data.msg)
          }
        })
      }
    },
    //母单发起约时工单时，自动以母单的最近派单人员进行约时工单的派单，并用短信通知处理人。
    fetchGetDisptachSheet() {
      if (this.form.data.IS_MOTHER_LAUNCH === 'yes') {
        if (this.request.fetchGetDisptachSheet) {
          const params = {
            MOTHER_APP_NO: this.form.data.MOTHER_APP_NO
          }
          this.loading = true
          this.request.fetchGetDisptachSheet(params).then(res => {
            this.loading = false
            if (!res.data.success) {
              this.$message.warning(res.data.msg)
            } else {
              this.fetchDisptachArrangeTime(res.data.fault95598DispatchSheet)
            }
          })
        }
      }
    },
    fetchDisptachArrangeTime(sheet) {
      if (sheet && this.request.fetchDisptachArrangeTime) {
        const d = this.form.data
        const params = {
          appNo: d.appNo,
          dispatchTeamId: sheet.dispatchTeamId, //派单班组id
          dispatchTeam: sheet.dispatchTeam, //派单班组
          dispatchHandlerId: sheet.dispatchHandlerId, //班组人员id
          dispatchHandler: sheet.dispatchHandler, //班组人员
          nameAndTel: '',
          isSendToCust: 'no', //是否发送给用户
          isSendToTeam: 'no', //是否发送班组负责人
          isSendToManager: 'no', //是否发送给客户经理
          smsCons: '',
          smsTeamManager: '',
          smsReaderManager: ''
        }
        this.loading = true
        this.request.fetchDisptachArrangeTime(params).then(res => {
          this.loading = false
          if (!res.data.success) {
            this.$message.warning(res.data.msg)
          }
        })
      }
    }
  }
}
</script>
