<template>
  <div>
    <Modal v-model="showModal" width="1200">
    <TwoColumnPage>
      <template slot="left">
        <ContractLeftList ref="leftModal" @on-result-change="_leftResult"></ContractLeftList>
      </template>
      <template slot="right">
        <ContractRightList ref="rightModal" @on-result-change="_rightResult"></ContractRightList>
      </template>
    </TwoColumnPage>

      <div slot="footer" class="btn-width">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>

<script>
import TwoColumnPage from '../../../components/base/TwoColumnPage'
import { meterItem } from '../../../api'
import ContractLeftList from './ContractLeftList'
import ContractRightList from './ContractRightList'

export default {
  components: {
    TwoColumnPage,
    ContractLeftList,
    ContractRightList
  },
  data() {
    return {
      showModal: false,
      sampleIds: '',
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '添加', type: 'primary' }
      ],
      itemIds: []
    }
  },
  methods: {
    _open(sampleIds) {
      this.showModal = true
      this.sampleIds = sampleIds
      this.$refs.leftModal._open()
    },
    _leftResult(data, personal) {
      this.$refs.rightModal._open(data, personal)
    },
    _rightResult(data) {
      if (undefined !== data) {
        this.itemIds = data
      }
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '添加':
          this._ok()
          break
      }
    },
    _cancel() {
      this.showModal = false
    },
    _ok() {
      if (this.itemIds.length === 0) {
        this.$Message.warning('请选择要添加的检测项目!')
        this.$refs.footerModal._hideLoading()
        return false
      }
      this._saveItemForSample({
        sampleIds: this.sampleIds,
        itemIds: this.itemIds
      })
    },
    _saveItemForSample: async function(data) {
      const result = await meterItem.saveItems(data)
      if (result) {
        this.$Message.success('添加成功!')
        this.$refs.footerModal._hideLoading()
        this.$emit('on-result-change')
        this._cancel()
      }
    }
  }
}
</script>
