<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>

          <a-col :span="24">
            <a-form-model-item label="委托信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weituoId">


              <a-select
                show-search
                v-model="model.weituoId"
                placeholder="请选择"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @change="handleChange"
              >
                <a-select-option v-for="d in weituoDictOptions" :key="d.value">
                  {{ d.text }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="委托样品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleIds">
              <a-select
                show-search

                mode="multiple"
                v-model="model.sampleIds"
                placeholder="请选择"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @change="handleYPChange"

              >
                <a-select-option v-for="d in yangppinDictOptions" :key="d.value">
                  {{ d.text }}
                </a-select-option>
              </a-select>
            </a-form-model-item>

          </a-col>

          <a-col :span="24">
            <a-form-model-item label="委托样品检测项目" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="testItemsIds">
              <a-select
                show-search
                mode="multiple"
                v-model="model.testItemsIds"
                placeholder="请选择"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"


              >
                <a-select-option v-for="d in yangppinLsDictOptions" :key="d.value">
                  {{ d.text }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="报告类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="baogaoType">

              <j-search-select-tag v-model="model.baogaoType" dict="itdm_moban,name,id"  />

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="环境温度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wendu">
              <a-input v-model="model.wendu" placeholder="请输入温度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="环境湿度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shidu">
              <a-input v-model="model.shidu" placeholder="请输入湿度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="环境大气压" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="daqiya">
              <a-input v-model="model.daqiya" placeholder="请输入大气压"  ></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ItdmBaogaoForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      yangppinLsDictOptions:[],
      weituoDictOptions: [],
      yangppinDictOptions: [],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        weituoId: [
          { required: true, message: '请输入委托id!' }
        ],
        sampleIds: [
          { required: true, message: '请输入委托样品id列表!' }
        ],
        testItemsIds: [
          { required: true, message: '请输入委托样品检测Id列表!' }
        ],
        baogaoType: [
          { required: true, message: '请输入报告类型!' }
        ]
      },
      url: {
        add: '/baogao/itdmBaogao/add',
        edit: '/baogao/itdmBaogao/edit',
        queryById: '/baogao/itdmBaogao/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    this.load()
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    load() {
      const select = {}
      getAction('/baogao/itdmBaogao//weituo/list', select).then((res) => {
        if (res.success) {
          this.weituoDictOptions = res.result.map(i => {
            const map = {}
            map.text = i.weituoNo
            map.value = i.id
            return map
          })
          console.log(this.weituoDictOptions)

        } else {
          this.$message.warning(res.message)
        }
      })
    },




    yangpmList(weituoId) {
      const select = { weituoId: weituoId }
      getAction('/baogao/itdmBaogao/yangpin/list', select).then((res) => {
        if (res.success) {
          this.yangppinDictOptions = res.result.map(i => {
            const map = {}
            map.text = i.sampleName
            map.value = i.id
            return map
          })
          console.log(this.weituoDictOptions)

        } else {
          this.$message.warning(res.message)
        }
      })
    },


    queryShiyanList(yangpinIds) {
      const select = { 'yangpinIds': yangpinIds.toString() }
      getAction('/baogao/itdmBaogao/shiyan/list', select).then((res) => {
        if (res.success) {
          this.yangppinLsDictOptions = res.result.map(i => {
            const map = {}
            map.text = i.testItems
            map.value = i.id
            return map
          })


        } else {
          this.$message.warning(res.message)
        }
      })
    },

    handleYPChange(value){
      console.log(value)
      this.queryShiyanList(value)
    },
    handleChange(value) {
      console.log(value)
      this.yangpmList(value)

      // this.value = value
      // fetch(value, data => (this.data = data))
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>