<template>
  <div>
    <Modal v-model="showModal" width="900" class="modal-footer-none zIndex-1100">
      <p slot="header">
        采购单详情
      </p>
      <div>
        <Row>
          <!--查询-->
          <Col span="24" />
          <Form inline onsubmit="return false" :label-width="70">
            <label class="label-sign" />
            <Form-item class="search-item" label="耗材编号:">
              <input v-model="orderId" type="hidden" name="orderId"></input>
              <Input v-model="formObj.chemicalNum" name="chemicalNum" placeholder="请输入耗材编号" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item class="search-item" label="耗材名称:">
              <Input v-model="formObj.chemicalName" name="chemicalName" placeholder="请输入耗材名称" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item class="search-item" label="供应商:">
              <Input v-model="formObj.supplier" name="supplier" placeholder="请输入供应商" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item class="search-item" label="规格:">
              <Input v-model="formObj.spec" name="spec" placeholder="请输入规格" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item class="search-item" label="制造厂商:">
              <Input v-model="formObj.manufacturer" name="manufacturer" placeholder="请输入制造厂商" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item class="search-btn">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <!-- 表格 -->
          <Col span="24" />
          <element-table
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            hide-checkbox
            @on-result-change="_tableResultChange"
          >
            <el-table-column
              v-for="item in pageColumns"
              :key="item.key"
              show-overflow-tooltip
              :prop="item.key"
              :label="item.title"
              :min-width="item.width"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
                <span v-if="item.key==='type'">
                  {{ scope.row[item.key] === 0 ? '流转' : scope.row[item.key] === 1 ? '退回': scope.row[item.key] === 2 ?'更新' :
                    scope.row[item.key] === 3 ? '转发':'文件' }}
                </span>
                <span v-else-if="item.key==='ctime'">{{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}</span>
                <span v-else>{{ scope.row[item.key] }}</span>
              </template>
            </el-table-column>
          </element-table>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <ModalFooter @on-result-change="_footerResult" :footer="footerList" ref="footerModal"></ModalFooter>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import { consumeRelPurchase } from '../../../api'
export default {
  data() {
    return {
      showModal: false,
      getPage: {},
      purchaseOrder: {},
      status: '',
      orderId: '',
      footerList: [],
      pageColumns: [
        { title: '耗材编号', key: 'chemicalNum', width: 100 },
        { title: '耗材名称', key: 'chemicalName', width: 120 },
        { title: '状态', key: 'stateDisplay', width: 120 },
        { title: '单价', key: 'price', width: 100, price: true },
        { title: '采购量', key: 'quantity', width: 120, quantity: true },
        { title: '已入库数量', key: 'alreadyQuantity', width: 140 },
        { title: '计量单位', key: 'unit', width: 100 },
        { title: '规格', key: 'spec', width: 100, spec: true },
        { title: '等级（纯度）', key: 'quality', width: 120, quality: true },
        { title: '供应商', key: 'supplier', width: 200, supplier: true },
        {
          title: '制造厂商',
          key: 'manufacturer',
          width: 200,
          manufacturer: true
        },
        { title: '备注', key: 'remark', width: 285, remark: true }
      ],
      formObj: {
        chemicalNum: '',
        chemicalName: '',
        supplier: '',
        spec: '',
        manufacturer: ''
      }
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _open(purchaseOrder) {
      this.showModal = true
      this.purchaseOrder = purchaseOrder
      this.orderId = this.purchaseOrder.id
      $('input[name=orderId]').val(this.purchaseOrder.id)
      this.modalTitle =
        purchaseOrder.num + '-' + (purchaseOrder.status === 0 ? '禁用' : '正常')

      this.status = purchaseOrder.status
      if (this.status === 0 || this.status === '审批驳回') {
        this.footerList = [
          { id: '', name: '取消', type: '' },
          { id: '', name: '提交审批', type: 'primary' }
        ]
      } else {
        this.footerList = [{ id: '', name: '取消', type: '' }]
      }
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const database = this.$serializeForm(this.formObj)
      console.log(database)
      const result = await consumeRelPurchase.page(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        // case 'iconClick':
        //   this._iconClick(data.name, data.rowData)
        //   break
        // case 'selectIds':
        //   this.selectIds = data
        //   break
      }
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>
