<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" @on-visible-change="_visibleChange" width="900">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form ref="formObj" :id="formId" :model="formObj" :rules="ruleValidate" :label-width="100" inline>
              <div>
                <Form-item label="属性名称" prop="attributeName" class="width-48">
                  <Input v-model="formObj.attributeName" name="attributeName" placeholder="请输入属性名称"/>
                </Form-item>
                <Form-item label="属性" prop="dataAttribute" class="width-48">
                  <Input v-model="formObj.dataAttribute" name="dataAttribute"
                         placeholder="请输入属性"/>
                </Form-item>
                <Form-item label="公式配置" prop="formula" class="width-48">
                  <Input v-model="formObj.formula" name="formula"
                         placeholder="请输入公式配置"/>
                </Form-item>
                <Form-item label="列位置" prop="columnPlace" class="width-48">
                  <Input
                    v-model="formObj.columnPlace"
                    @keydown.native="channelInputLimit"
                    name="columnPlace"
                    type="number"
                    onmousewheel="return false"
                    placeholder="请输入列位置"
                  ></Input>
                </Form-item>

                <Form-item label="合并开始列" prop="mergeBegin" class="width-48">
                  <Input
                    v-model="formObj.mergeBegin"
                    @keydown.native="channelInputLimit"
                    name="mergeBegin"
                    type="number"
                    onmousewheel="return false"
                    placeholder="请输入合并开始列"
                  />
                </Form-item>

                <Form-item label="合并结束列" prop="mergeEnd" class="width-48">
                  <Input
                    v-model="formObj.mergeEnd"
                    @keydown.native="channelInputLimit"
                    name="mergeEnd"
                    type="number"
                    onmousewheel="return false"
                    placeholder="请输入合并结束列"
                  />
                </Form-item>
                <Form-item label="合并行数" prop="mergeRowNum" class="width-48">
                  <Input
                    v-model="formObj.mergeRowNum"
                    @keydown.native="channelInputLimit"
                    name="mergeRowNum"
                    type="number"
                    onmousewheel="return false"
                    placeholder="请输入合并行数"
                  />
                </Form-item>
                <Form-item label="是否是项目指标" prop="itemed" class="width-48">
                  <Select v-model="formObj.itemed"  @change="_optionChange" placeholder="请选择">
                    <Option
                      v-for="(item,index) in options"
                      :key="index"
                      :label="item.value"
                      :value="index"
                    />
                  </Select>
                </Form-item>
                <Form-item label="单位" prop="unit" class="width-48">
                  <Input v-model="formObj.unit" name="unit"
                         placeholder="请输入单位"/>
                </Form-item>
              </div>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></modal-footer>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑环境资质项目表
 */

import { soilAptitude } from '../../../api'

export default {
  components: {},
  data() {
    return {
      activeName: '',
      hideUserSel: true,
      id: '',
      modalTitle: '',
      templateId: '',
      formObj: {
        attributeName: undefined,
        formula: undefined,
        columnPlace: '',
        unit: undefined,
        status: undefined,
        mainType: undefined,
        smallType: undefined,
        testValue: undefined,
        mergeBegin: '',
        mergeRowNum: '',
        mergeEnd: '',
        itemed: undefined
      },
      options: [
        {
          label: '否',
          value: '否'
        },
        {
          label: '是',
          value: '是'
        }
      ],
      ruleValidate: {
        name: [{ required: true, message: '指标名称不能为空', trigger: 'blur' }]
      },
      showModal: false,
      footerList: [
        { name: '取消', type: '' },
        { name: '保存', type: 'primary' }
      ],
      microbedList: [{ value: 0, name: '否' }, { value: 1, name: '是' }],
      // 检测依据
      testBasisData: [],
      // 类别下拉数组
      typeData: [],
      // 检测科室
      groupData: [],
      groupName: '',
      // 主检人
      userData: [],
      // 单位
      unitData: [],
      aptitudeData: [],

      aptitude: '',
      // testBasisName: '',
      computeTypeList: [],
      tester: '',
      marksList: [],
      detectionTypeList: [],
      testItemData: [],
      formId: ''
    }
  },
  mounted() {
    // 检测科室
    // this._getUserGroup()
    // 检测依据
    // this._getTestBasisList()
    // 资质
    // this._getAptitudeList()
    // this._getType()
  },
  methods: {
    channelInputLimit(e) {
      const key = e.key
      // 不允许输入'e'和'.'
      if (key === 'e' || key === '.') {
        e.returnValue = false
        return false
      }
      return true
    },
    inputChange(data) {
      console.log('data>>>', data)
      console.log('formObj>>>', this.formObj)
    },
    _selectJudgeType(data) {
      this.formObj.microbed = data
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open(id, formObj) {
      this.formId = 'edit-form-template-config'
      this._hideLoading()
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.templateId = id
        this.modalTitle = '添加模板配置'
        console.log('formObj>>>', this.formObj)
      } else {
        this.hideUserSel = true
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑模板配置'
        console.log('formObj>>>', this.formObj)
      }
      this.showModal = true
    },
    // 获取表数据
    _resultChange(msg) {
      this.showModal = false
      this.$refs.footerModal._hideLoading()
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this.$refs.footerModal._hideLoading()
    },
    _optionChange(data) {
      this.formObj.itemed = data
      console.log(this.formObj.itemed)
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form-template-config')
          if (this.$string(this.id).isEmpty()) {
            data.templateId = this.templateId
            data.itemed = this.formObj.itemed
            // 添加
            this._save(data)
          } else {
            // 编辑
            data.itemed = this.formObj.itemed
            this._edit({ id: this.formObj.id, obj: data })
          }
        } else {
          this.$refs.footerModal._hideLoading()
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      console.log(data)
      const result = await soilAptitude.saveOriginalTemplateConfig(data)
      console.log(result)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.editOriginalTemplateConfig(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _cancel() {
      this.showModal = false
      this._hideLoading()
    },
    _visibleChange(data) {
      if (!data && this.$showBtn('food-aptitude-item-step-edit')) {
        this.$emit('on-result-change')
        this.$refs.testStepByAptitude._clear()
      }
    }
  }
}
</script>
