<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="1000">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90" inline>
          <Form-item label="实验室名称" prop="name" class="width-48">
            <Input v-model="formObj.name" name="name" placeholder="请输入实验室名称"/>
          </Form-item>
          <Form-item label="资质:" prop="aptitude" class="width-48">
            <Select v-model="formObj.aptitude" name="aptitude" multiple placeholder="请选择资质">
              <Option v-for="item in aptitudeData" :key="item.name" :value="item.name">{{item.name}}</Option>
            </Select>
          </Form-item>
          <Form-item label="联系人：" prop="linkman" class="width-48">
            <Input v-model="formObj.linkman" name="linkman" placeholder="请输入联系人"/>
          </Form-item>
          <Form-item label="手机号：" prop="tel" class="width-48">
            <Input v-model="formObj.tel" name="tel" placeholder="请输入手机号"/>
          </Form-item>
          <Form-item label="邮箱：" prop="email" class="width-48">
            <Input v-model="formObj.email" name="email" placeholder="请输入邮箱"/>
          </Form-item>
          <Form-item label="地址：" prop="address" class="width-48">
            <Input v-model="formObj.address" name="address" placeholder="请输入地址"/>
          </Form-item>
          <Form-item label="结算方式:" prop="clearingWay" class="width-48">
            <AutoCompletes name="clearingWay" :value="formObj.clearingWay" :down-data="clearingWayList"
                           placeholder="请输入或选择结算方式" @on-result-change="_wayResult"></AutoCompletes>
          </Form-item>
          <Form-item label="评价表编号：" prop="assessFormSn" class="width-48">
            <Input v-model="formObj.assessFormSn" name="assessFormSn" placeholder="请输入评价表编号"/>
          </Form-item>
          <Form-item label="评价人员：" prop="assessPerson" class="width-48">
            <Input v-model="formObj.assessPerson" name="assessPerson" placeholder="请评价人员"/>
          </Form-item>
          <Form-item label="人员情况：" prop="personCondition" style="width: 99.8%">
            <Input v-model="formObj.personCondition" name="personCondition" type="textarea"
                   :rows="3" placeholder="请输入人员情况"/>
          </Form-item>
          <Form-item label="分包范围：" prop="subArea" style="width: 99.8%">
            <Input v-model="formObj.subArea" name="subArea" type="textarea"
                   :rows="3" placeholder="请输入分包范围"/>
          </Form-item>
          <Form-item label="备注：" prop="remark" style="width: 99.8%">
            <Input v-model="formObj.remark" name="remark" type="textarea" :rows="3"
                   placeholder="请输入备注"/>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></modal-footer>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑分包商
 */
import AutoCompletes from '../../../components/base/AutoCompletes'
import { lmsBaseDict, meterSubcontractor } from '../../../api'

export default {
  components: {
    AutoCompletes
  },
  data() {
    return {
      id: '',
      modalTitle: '',
      subcontractorId: '',
      formObj: {
        name: '',
        linkman: '',
        tel: '',
        email: '',
        address: '',
        remark: '',
        clearingWay: '',
        aptitude: [],
        assessFormSn: '',
        subArea: '',
        personCondition: '',
        assessPerson: ''
      },
      ruleValidate: {
        name: [
          { required: true, message: '实验室名称不能为空', trigger: 'blur' }
        ]
      },
      clearingWayList: ['款到后测试', '先测试后付款', '其他情况（）'],
      clearingWayListTemp: ['款到后测试', '先测试后付款', '其他情况（）'],
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      aptitudeData: []
    }
  },
  methods: {
    /** *modal-footer */
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(msg) {
      this.showModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          this.$extend(data, { aptitude: this.formObj.aptitude.toString() })
          if (this.$string(this.id).isEmpty()) {
            // 添加
            this._save(data)
          } else {
            // 编辑
            this._edit({ id: this.formObj.id, obj: data })
          }
        } else {
          this.$Message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _wayResult(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.clearingWay = data
          break
        case 'query':
          this.formObj.clearingWay = data
          this._wayQuery(data)
          break
      }
    },
    _wayQuery(query) {
      if (query === '') {
        this.clearingWayList = this.clearingWayListTemp
      } else {
        this.clearingWayList = this.clearingWayListTemp.filter(
          item => item.indexOf(query) > -1
        )
      }
    },
    _cancel() {
      this.showModal = false
    },
    _open(formObj) {
      this.showModal = true
      this.$refs.formObj.resetFields()
      this._hideLoading()
      this._getAptitudeList()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.formObj.aptitude = []
        this.modalTitle = '分包商管理新增'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '分包商管理编辑'
        this._showTime(formObj)
      }
    },
    _getAptitudeList: async function() {
      const result = await lmsBaseDict.getItem('subcontractorAptitude')
      if (result) {
        this.aptitudeData = result
      }
    },
    _showTime(formObj) {
      // 回显资质类型
      if (formObj.aptitude !== undefined || '') {
        this.formObj.aptitude = formObj.aptitude.split(',')
      } else {
        this.formObj.aptitude = []
      }
    },
    _save: async function(data) {
      const result = await meterSubcontractor.save(data)
      console.log(result)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await meterSubcontractor.edit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    }
  }
}
</script>
