<template>
  <div>
    <Modal v-model="showEditModal" :mask-closable="false">
      <p slot="header">{{modalTitle}}</p>
      <div v-show="isLoad">
        <Spin fix>
          <Icon type="load-c" size=18 class="file-spin-icon-load"></Icon>
          <div>正在上传,请稍后...</div>
        </Spin>
      </div>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="95" inline class="inputBox">
          <Form-item label="类别" prop="classType" style="width: 100%">
<!--            <Input v-model="formObj.classType" @on-keyup="_onKeyUp" name="classType" placeholder="请选择类别" icon="plus-circled"-->
<!--                   ></Input>-->
            <el-select v-model="formObj.classType" placeholder="请选择类别" size="small" clearable>
              <el-option :label="item.value" :value="item.value" v-for="(item,index) in typeList"
                         :key="index"></el-option>
            </el-select>
          </Form-item>
          <Form-item label="原始记录名称" prop="alias" style="width: 100%">
            <Input v-model="formObj.alias" name="alias" placeholder="请输入原始记录名称"></Input>
          </Form-item>
          <Form-item label="备注" prop="remark" style="width: 100%">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注"></Input>
          </Form-item>
          <Form-item label="样品开始行" prop="sampleBeginRow" style="width: 100%">
            <el-input
              v-model="formObj.sampleBeginRow"
              @keydown.native="channelInputLimit"
              name="sampleBeginRow"
              type="number"
              onmousewheel="return false"
              placeholder="请输入样品开始行"
            />
          </Form-item>
          <Form-item label="样品合并数" prop="sampleMergerNum" style="width: 100%">
            <el-input
              v-model="formObj.sampleMergerNum"
              @keydown.native="channelInputLimit"
              name="sampleMergerNum"
              type="number"
              onmousewheel="return false"
              placeholder="请输入样品合并数"
            />
          </Form-item>
          <Form-item label="模板样品数" prop="templateSampleNum" style="width: 100%">
            <el-input
              v-model="formObj.templateSampleNum"
              @keydown.native="channelInputLimit"
              name="templateSampleNum"
              type="number"
              onmousewheel="return false"
              placeholder="请输入模板样品数"
            />
          </Form-item>
          <Form-item label="多sheet模板" prop="moreSheet" style="width: 100%">
            <Radio-group v-model="formObj.moreSheet">
              <Radio v-for="item in sheetType" :key="item.value" :label="item.value">{{item.name}}</Radio>
            </Radio-group>
          </Form-item>
        </Form>
      </div>
      <div slot="footer" class="btn-width clearfix">
        <Button @click="_cancel" style="margin-left: 8px;">取消</Button>
        <Button @click="_ok" type="primary" style="margin-bottom: 0;float: right">保存</Button>
<!--        <label style="margin-bottom: 0;float: right">-->
<!--          <Upload-->
<!--            :action="action"-->
<!--            :on-success="_handelsuccess"-->
<!--            :before-upload="_beupload"-->
<!--            :on-progress="_handelprogress"-->
<!--            :on-format-error="_formatError"-->
<!--            :data="dataObj"-->
<!--            :with-credentials="true"-->
<!--            :show-upload-list="false"-->
<!--            :format="format"-->
<!--          >-->
<!--            <Button :disabled="isDisable" type="primary">导入文件</Button>-->
<!--          </Upload>-->
<!--        </label>-->
      </div>
    </Modal>
    <!--类别弹出树-->
<!--    <LmsTemplateClassifyZTree ref="ztreeModal" @on-result-change="_ztree"></LmsTemplateClassifyZTree>-->
  </div>
</template>
<script>
/**
 * 添加编辑
 */
// import LmsTemplateClassifyZTree from '../lms-template-classify/LmsTemplateClassifyZTree.vue'
// import global from '../../../api/config'
import { soilAptitude } from '../../api'

const defVal = {
  alias: '',
  classType: '',
  remark: '',
  sampleBeginRow: '',
  sampleBeginCol: '',
  sampleMergerNum: '',
  templateSampleNum: '',
  moreSheet: 0,
  classifyId: 0
}

export default {
  // components: { LmsTemplateClassifyZTree },
  data() {
    const validatesampleBeginRow = (rule, value, callback) => {
      if (this.formObj.sampleBeginRow === '') {
        callback(new Error('内容不能为空'))
      } else {
        callback()
      }
    }
    const validatesampleMergerNum = (rule, value, callback) => {
      if (this.formObj.sampleMergerNum === '') {
        callback(new Error('内容不能为空'))
      } else {
        callback()
      }
    }
    const validatetemplateSampleNum = (rule, value, callback) => {
      if (this.formObj.templateSampleNum === '') {
        callback(new Error('内容不能为空'))
      } else {
        callback()
      }
    }
    const validatesampleBeginCol = (rule, value, callback) => {
      if (this.formObj.sampleBeginCol === '') {
        callback(new Error('内容不能为空'))
      } else {
        callback()
      }
    }
    return {
      modalTitle: '编辑',
      formObj: defVal,
      ruleValidate: {
        alias: [
          { required: true, message: '模板名称不能为空', trigger: 'blur' }
        ],
        sampleBeginRow: [
          {
            required: true,
            validator: validatesampleBeginRow,
            message: '样品开始行不能为空',
            trigger: 'blur'
          }
        ],
        sampleBeginCol: [
          {
            required: true,
            validator: validatesampleBeginCol,
            message: '样品开始列不能为空',
            trigger: 'blur'
          }
        ],
        sampleMergerNum: [
          {
            required: true,
            validator: validatesampleMergerNum,
            message: '样品合并数不能为空',
            trigger: 'blur'
          }
        ],
        templateSampleNum: [
          {
            required: true,
            validator: validatetemplateSampleNum,
            message: '模板样品数不能为空',
            trigger: 'blur'
          }
        ],
        classType: [
          { required: true, message: '类别不能为空', trigger: 'blur' }
        ]
      },
      sheetType: [{ value: 1, name: '是' }, { value: 0, name: '否' }],
      typeList: [
        {
          value: '力学'
        },
        {
          value: '物性'
        },
        {
          value: '高级'
        }
      ],
      id: '',
      showEditModal: false,
      dataObj: {
        file: ''
      },
      pname: '',
      action: '',
      isLoad: false,
      isDisable: true,
      format: ['docx', 'xlsx', 'xls']
    }
  },
  methods: {
    _onKeyUp() {
      if (this.formObj.alias === '' || this.formObj.classType === '') {
        this.isDisable = true
      } else {
        this.isDisable = false
        this.action =
          global.baseURL +
          '/meter/v1/file_template/upload_template/?classType=' +
          this.formObj.classType +
          '&alias=' +
          this.formObj.alias +
          '&remark=' +
          this.formObj.remark
      }
    },
    channelInputLimit(e) {
      const key = e.key
      // 不允许输入'e'和'.'
      if (key === 'e' || key === '.') {
        e.returnValue = false
        return false
      }
      return true
    },
    _selectZtree() {
      // this.$refs.ztreeModal._openZtree()
    },
    _cancel() {
      this.showEditModal = false
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        console.log('valid', valid)
        if (valid) {
          this._editOk()
        } else {
          this.$Message.error('请输入必填值')
        }
      })
    },
    _editOk: async function() {
      const result = await soilAptitude.originalTemplateEdit({
        id: this.id,
        formObj: this.formObj
      })
      if (result) {
        this.$message.success('编辑成功')
        this.showEditModal = false
        this.$emit('on-result-change')
      }
    },
    _open(data) {
      console.log(data)
      this.showEditModal = true
      this.isLoad = false
      this.id = data.id
      this.formObj.alias = data.alias
      this.formObj.remark = data.remark
      this.formObj.classType = data.classType
      this.formObj.sampleBeginRow = data.sampleBeginRow
      this.formObj.sampleBeginCol = data.sampleBeginCol
      this.formObj.sampleMergerNum = data.sampleMergerNum
      this.formObj.templateSampleNum = data.templateSampleNum
      this.formObj.moreSheet = data.moreSheet
      this.modalTitle = '编辑模板'
      console.log(this.id)
    },
    _beupload(file) {
      this.dataObj.file = file.name
    },
    _handelprogress(event, file, fileList) {
      this.isLoad = true
    },
    // _handelsuccess(response, file, fileList) {
    //   // 上传成功
    //   if (response.success) {
    //     this.isLoad = false
    //     this.$Message.success('上传成功！')
    //     this.$emit('on-result-change')
    //     this.showEditModal = false
    //   } else {
    //     this.isLoad = true
    //     this.$Message.error(response.msg)
    //   }
    // },
    _formatError(file) {
      this.$Notice.error({
        title: '文件格式不正确',
        duration: 10,
        desc:
          '文件 ' +
          file.name +
          ' 格式不正确，请上传格式为 docx、xlsx、xls 的文件！'
      })
    },
    _ztree(result) {
      this.pname = ''
      if (result === undefined) {
        this.formObj.classifyId = '0'
        this.pname = ''
      } else {
        this.formObj.classifyId = result.id
        this.pname = result.name
      }
      // 判断必填项是否为空
    }
  }
}
</script>
<style scoped>
.clearfix:after {
  content: '';
  clear: both;
  display: block;
}
.inputBox >>> input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
.inputBox >>> input[type='number'] {
  -moz-appearance: textfield !important;
}
</style>
