<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="true" width="1000px">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form
          id="right-edit-form-edit"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="客户名称" prop="customerName" class="width-48">
            <Tooltip content="长度为0-50" placement="bottom-start">
              <Input
                v-model="formObj.customerName"
                name="customerName"
                :maxlength="50"
                placeholder="请输入客户名称"
                style="width: 355px;"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="联系人" prop="customerContact" class="width-48">
            <Tooltip content="长度为0-20" placement="bottom-start">
              <Input
                v-model="formObj.customerContact"
                name="customerContact"
                :maxlength="20"
                placeholder="请输入联系人"
                style="width: 355px;"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="地址" prop="customerAddress" class="width-48">
            <Tooltip content="长度为0-100" placement="bottom-start">
              <Input
                v-model="formObj.customerAddress"
                name="customerAddress"
                :maxlength="100"
                placeholder="请输入地址"
                style="width: 355px;"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="电话" prop="customerTel" class="width-48">
            <Input v-model="formObj.customerTel" name="customerTel" placeholder="请输入电话" /></Input>
          </Form-item>
          <Form-item label="填表日期" prop="registerTime" class="width-48">
            <Date-picker
              v-model="formObj.registerTime"
              name="registerTime"
              type="date"
              placeholder="请输入填表时间"
              format="yyyy-MM-dd"
              style="width: 100%;"
            />
          </Form-item>
          <Form-item label="服务项目" prop="serviceItems" class="width-48">
            <Tooltip content="长度为0-200" placement="bottom-start">
              <Input
                v-model="formObj.serviceItems"
                name="serviceItems"
                :maxlength="200"
                placeholder="请输入服务项目"
                style="width: 355px;"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="客户满意度结论" prop="customerResult" >
            <!--            <Editor ref="editorModal" v-model="formObj.customerResult" @on-result-change="_editorData" />-->
            <Input
              v-model="formObj.customerResult"
              name="serviceItems"
              :maxlength="200"
              placeholder="请输入客户满意度结论"
              style="width: 355px;"
            ></Input>
          </Form-item>
          <Form-item label="其他意见和建议" prop="customerAdvice" class="width-48">
            <Tooltip content="长度为0-200" placement="bottom-start">
              <Input
                v-model="formObj.customerAdvice"
                name="customerAdvice"
                placeholder="请输入其他意见和建议"
                style="width: 360px"
                :maxlength="200"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="备注" prop="remark" class="width-48">
            <Tooltip content="长度为0-100" placement="bottom-start">
              <Input
                v-model="formObj.remark"
                name="remark"
                placeholder="请输入备注"
                style="width: 360px"
                :maxlength="100"
              ></Input>
            </Tooltip>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>

    <!--选择人员-->
    <UserInfo ref="userModal" is-change @on-result-change="_userData" />
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { dict, staffInfo, customerInvestigate } from '../../../api'
import UserInfo from '../../../components/user-info-single/assignPerson'
export default {
  components: { UserInfo },
  data: function() {
    const validateTel = (rule, value, callback) => {
      // 验证手机号
      const reg = /^((\+?86)|(\(\+86\)))?1\d{10}$/
      if (value === '') {
        callback()
      } else if (!reg.test(this.formObj.customerTel)) {
        callback(new Error('请输入正确格式的电话号码！'))
      } else {
        callback()
      }
    }
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        customerName: undefined,
        customerAddress: undefined,
        customerContact: undefined,
        customerTel: undefined,
        serviceItems: undefined,
        registerTime: undefined,
        customerResult: undefined,
        customerAdvice: undefined,
        remark: undefined,
        lmsFrameFileList: []
      },
      innerAuditList: [],
      userList: [],
      ruleValidate: {
        customerTel: [
          { required: true, validator: validateTel, trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _selectApply() {
      this.$refs.userModal._open('registerName')
    },
    _userData(data, msg) {
      // 选择人员返回数据
      if (msg === 'registerName') {
        // 选择取样人取样人
        this.formObj.registerName = data.realname
        this.formObj.registerId = data.userId
      }
    },
    _getInnerAuditList: async function() {
      const result = await dict.getItem('内审审核类型')
      this.innerAuditList = result
    },
    _selectPlan() {
      this.$refs.trainPlanModal._open()
    },

    _backTrainPlanData(data) {
      this.formObj.trainPlan = data.name
      this.formObj.trainId = data.id
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          this._save(this.formObj)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },

    _checkMemberNameChange(data) {
      this.formObj.checkMemberName = data.label.trim()
      this.formObj.checkMemberId = data.value.trim()
    },
    // 添加、编辑保存
    _save: async function(data) {
      if (data.id === undefined || data.id === '') {
        const result = await customerInvestigate.add(data)
        this._resultChange(result, '添加成功')
      } else {
        const result = await customerInvestigate.edit({
          id: data.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _getUserList: async function() {
      const result = await staffInfo.list({ status: 1 })
      if (result) {
        this.userList = result
      }
    },
    _leaderChange(obj) {
      console.log(obj)
      if (obj) {
        this.formObj.checkGroupLeaderName = obj.label.trim()
      } else {
        this.formObj.checkGroupLeaderName = ''
      }
    },

    _memberChange(obj) {
      let names = ''
      let ids = ''
      obj.forEach(function(item) {
        names = names + item.label.trim() + ','
        ids = ids + item.value + ','
      })
      this.formObj.checkGroupMemberNames = names.substring(0, names.length - 1)
      this.formObj.checkGroupMemberIds = ids.substring(0, ids.length - 1)
    },
    _open(formObj) {
      this.$refs.formObj.resetFields()
      // const user = global.getUserInfo()
      // console.log('----user-----', user)
      // this.$refs.formObj.resetFields()
      // this.formObj.registerId = user.id
      // this.formObj.registerName = user.realname
      this.showEditModal = true
      this._hideLoading()
      if (formObj === '' || undefined === formObj) {
        this.id = ''
        this.modalTitle = '添加内审实施管理'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑内审实施管理'
        if (undefined !== formObj.registerTime) {
          this.formObj.registerTime = this.$dateformat(
            new Date(formObj.registerTime),
            'yyyy-mm-dd'
          )
        } else {
          this.formObj.registerTime = ''
        }
        if (undefined !== formObj.ctime) {
          this.formObj.ctime = this.$dateformat(
            new Date(formObj.ctime),
            'yyyy-mm-dd'
          )
        } else {
          this.formObj.ctime = ''
        }
      }
    }
  }
}
</script>
