<template>
  <div>
    <Modal v-model="showStockModal" title="归类" width="1200">
      <!--内容-->
      <div>
        <Row>
          <Col span="2" />
          <Card class="card_item">
            <p slot="title">
              未添加归类
            </p>
            <div>
              <LmsChemicalMakeClassify
                ref="chemicalMakeClassifyModal"
                @on-result-change="_searchChemical"
              />
            </div>
          </Card>
          </Col>
          <Col span="2" />
          <div style="text-align: center;margin-top:150px">
            <div style="margin-bottom: 20px">
              <Tooltip content="添加" transfer placement="right">
                <Button type="primary" @click="_submit">
                  <Icon type="chevron-right" />
                </Button>
              </Tooltip>
            </div>
            <div>
              <Tooltip content="移除" transfer placement="right">
                <Button type="primary" @click="_deleteSelected">
                  <Icon type="chevron-left" />
                </Button>
              </Tooltip>
            </div>
          </div>
          </Col>
          <Col span="11" />
          <Card class="card_item">
            <p slot="title">
              已添加归类
            </p>
            <div>
              <Row>
                <Col span="24" />
                <Form id="search-form-classify" inline onsubmit="return false">
                  <label class="label-sign" />
                  <Form-item>
                    归类名称：<Input
                      name="name"
                      placeholder="请输入归类名称"
                      style="width: 160px;"
                      @on-enter="_pageChange(1)"
                    ></Input>
                  </Form-item>
                  <input name="classifyed" type="hidden" value="0"></input>
                  <Form-item>
                    <Button type="primary" @click="_pageChange(1)">
                      搜索
                    </Button>
                  </Form-item>
                </Form>
                </Col>
                <Col span="24" />
                <element-table
                  ref="pageTable"
                  :page-columns="pageColumns"
                  :table-height="tableHeight"
                  :get-page="getPage"
                  :icon-msg="iconMsg"
                  :opt-col-width="70"
                  @on-result-change="_tableResultChange"
                >
                  <el-table-column
                    v-for="item in pageColumns"
                    :key="item.key"
                    show-overflow-tooltip
                    sortable
                    :prop="item.key"
                    :label="item.title"
                    :min-width="item.width"
                    :fixed="item.fixed?item.fixed:undefined"
                  >
                    <template slot-scope="scope">
                      <div v-if="item.detail">
                        <a @click.stop="_detailModal(scope.row.id)">{{ scope.row[item.key] }}</a>
                      </div>
                      <div v-else>
                        {{ scope.row[item.key] }}
                      </div>
                    </template>
                  </el-table-column>
                </element-table>
                </Col>
              </Row>
            </div>
          </Card>
          </Col>
        </Row>
      </div>
    </Modal>
    <!--耗材详情-->
    <LmsChemicalClassifyDetail ref="detailModal" />
  </div>
</template>
<script>
import LmsChemicalMakeClassify from './LmsChemicalMakeClassify.vue'
import LmsChemicalClassifyDetail from './LmsChemicalClassifyDetail.vue'
export default {
  components: {
    LmsChemicalMakeClassify,
    LmsChemicalClassifyDetail
  },
  data() {
    return {
      makeClassIdsData: {
        classPks: [],
        chemicalPks: []
      },
      selectIds: [],
      chemicalClassIds: [],
      classIds: [],
      pageParams: {
        rows: 20
      },
      showStockModal: false,
      pageColumns: [
        { title: '归类名称', key: 'name', width: 120 },
        { title: '归类库存量', width: 120, key: 'stock' },
        { title: '计量单位', width: 100, key: 'unit' },
        { title: '预警库存量', key: 'warning', width: 130 },
        { title: '备注', key: 'remark' }
      ],
      iconMsg: [{ type: 'trash-a', id: 'chemical-manage-del', name: '移除' }],
      // chemicalId: '',
      DATA: {
        // 删除要提交的数据
        chemicalId: '',
        classifyIds: [],
        unit: ''
      },
      getPage: {},
      // 未归类数据
      selectedIds: []
    }
  },
  computed: {
    tableHeight: function() {
      return '400'
    }
  },
  methods: {
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        default:
          this._page()
      }
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this.selectedIds = []
      this.selectIds = []
      this._open(this.DATA.chemicalId, this.DATA.unit)
    },
    // 添加
    _submit() {
      if (this.selectedIds.length === 0) {
        this.$Message.warning('请至少选择一条要进行归类类别！')
      } else {
        // const tempData = {
        //   classifyIds: this.selectedIds,
        //   chemicalId: this.DATA.chemicalId
        // }
        // this.$store
        //   .dispatch('LmsChemicalClassify/chemicalClass', tempData)
        //   .then(() => {
        //     this._resultChange('添加成功')
        //   })
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '移除':
          this._deleteByIds([data.id])
          break
      }
    },
    _page() {
      // this.$store
      //   .dispatch('LmsChemicalClassify/getIncludePage', this._searchParams())
      //   .then(() => {
      //     this.loading = false
      //     this.getPage = this.$store.state.LmsChemicalClassify.includePage
      //   })
    },
    _pageChange(page) {
      this.pageParams.page = page
      this._page()
    },
    _deleteByIds(ids, content) {
      this.DATA.classifyIds = ids
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定移除该记录？',
        onOk: () => {
          // this.$store
          //   .dispatch('LmsChemicalClassify/chemicalClassDel', this.DATA)
          //   .then(() => {
          //     this._resultChange('移除成功')
          //   })
        }
      })
    },
    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      this.DATA.classifyIds = ids
      if (ids === '') {
        this.$Message.warning('请选择一条或多条已归类数据！')
      } else {
        this._deleteByIds(ids, '确定移除 ' + ids.length + ' 条记录？')
      }
    },
    _detailModal(id) {
      // 查看
      // this.$store.dispatch('LmsChemicalClassify/getById', id).then(() => {
      //   this.$refs.detailModal._open(
      //     this.$store.state.LmsChemicalClassify.model
      //   )
      // })
    },
    _search() {
      this._page()
    },
    _searchChemical(data, single) {
      if (single === undefined || single === '') {
        this.selectedIds = data
      } else {
        // const tempData = {
        //   classifyIds: data,
        //   chemicalId: this.DATA.chemicalId
        // }
        // this.$store
        //   .dispatch('LmsChemicalClassify/chemicalClass', tempData)
        //   .then(() => {
        //     this._resultChange('添加成功')
        //   })
      }
      // this._page();
      // this.$emit("on-result-change");
    },
    _searchParams() {
      const data = this.$serialize('search-form-classify')
      return this.$extend(data, this.pageParams)
    },
    _open(id, exunit) {
      this.showStockModal = true
      this.pageParams.chemicalId = id
      this.DATA.chemicalId = id
      this.DATA.unit = exunit
      this.$refs.chemicalMakeClassifyModal._open(
        this.DATA.chemicalId,
        this.DATA.unit
      )
      this._page()
    }
    // _btnClick(msg) {
    //   switch (msg) {
    //     case '添加' :
    //       this._addChemical();
    //       break;
    //     case '移除' :
    //       this._deleteSelected();
    //       break;
    //   }
    // },
  }
}
</script>
