<!--检测项目编辑列表（添加、导入检测项目）-->
<template>
  <div>
    <Modal
      v-model="showModal"
      v-drag
      :mask-closable="false"
      title="采购单"
      :width="1200"
      footer-hide
    >
      <Row>
        <Col span="24" />
        <Form v-model="formObj" inline onsubmit="return false" :label-width="90">
          <Form-item class="search-item" label="耗材编号:">
            <Input name="chemicalNum" placeholder="请输入耗材编号" @on-enter="_formSearch" /></Input>
          </Form-item>
          <Form-item class="search-item" label="耗材名称:">
            <Input name="chemicalName" placeholder="请输入耗材名称" @on-enter="_formSearch" /></Input>
          </Form-item>
          <Form-item class="search-item" label="供应商:">
            <Input name="supplier" placeholder="请输入供应商" @on-enter="_formSearch" /></Input>
          </Form-item>
          <Form-item class="search-item" label="规格:">
            <Input name="spec" placeholder="请输入规格" @on-enter="_formSearch" /></Input>
          </Form-item>
          <Form-item class="search-item" label="制造厂商:">
            <Input name="manufacturer" placeholder="请输入制造厂商" @on-enter="_formSearch" /></Input>
          </Form-item>
          <Form-item class="search-btn">
            <Button type="primary" @click="_formSearch">
              搜索
            </Button>
          </Form-item>
        </Form>
        </Col>
        <!--正常界面-->
        <Col span="24" />
        <Button type="primary" style="margin-bottom: 5px" @click="_delete">
          删除
        </Button>
        <Button type="primary" style="margin-bottom: 5px" @click="_export">
          导出
        </Button>
        <Button type="primary" style="margin-bottom: 5px" @click="_print">
          打印
        </Button>
        </Col>
        <!-- 表格 -->
        <Col span="24" />
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          select-data
          :table-height="350"
          :get-page="getPage"
          :icon-msg="iconMsg"
          :opt-col-width="80"
          show-check-box
          @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"
            :width="item.width"
            :min-width="200"
          >
            <template slot-scope="scope">
              <span v-if="item.key==='judged'">
                {{ scope.row[item.key]===1?'是':'否' }}
              </span>
              <div v-else-if="item.date">
                {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd '):'' }}
              </div>
              <div v-else-if="item.key === 'price'" @click="_handleRow(scope)">
                <el-input
                  v-model="scope.row.price"
                  size="small"
                  type="number"
                  @focus="_handleRow(scope)"
                  @blur="_blurSave(item.key)"
                />
              </div>
              <div v-else-if="item.key === 'quantity'" @click="_handleRow(scope)">
                <el-input
                  v-model="scope.row.quantity"
                  size="small"
                  @focus="_handleRow(scope)"
                  @blur="_blurSave(item.key)"
                />
              </div>
              <div v-else-if="item.key === 'remark'" @click="_handleRow(scope)">
                <el-input
                  v-model="scope.row.remark"
                  size="small"
                  @focus="_handleRow(scope)"
                  @blur="_blurSave(item.key)"
                />
              </div>
              <span
                v-else-if="item.key==='orderBy'"
                @click.stop="_handleRow(scope)"
              >
                <el-input-number
                  v-model="scope.row.orderBy"
                  size="small"
                  placeholder="排序"
                  @change="_handleOrderValueEdit(scope.row.id,scope.row.orderBy)"
                />
              </span>
              <span v-else>
                {{ scope.row[item.key] }}
              </span>
            </template>
          </el-table-column>
        </element-table>
        </Col>
      </Row>
      <!--      <div slot="footer">-->
      <!--        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />-->
      <!--      </div>-->
      <Button type="" style="margin-left: 73%" @click="_cancel">
        取消
      </Button>
      <Button type="primary" @click="_saveOrder('false')">
        保存
      </Button>
      <Button type="primary" @click="_submitOrderCheck">
        提交审批
      </Button>
    </Modal>

    <!--申请原因-->
    <Modal v-model="showApplyModal" :mask-closable="false">
      <p slot="header">
        {{ applyTitle }}
      </p>
      <div>
        <Form ref="applyFormObj" :model="applyFormObj" :rules="ruleValidate">
          <Form-item label="申请原因" prop="remark" style="width: 98%">
            <Input v-model="applyFormObj.remark" type="textarea" :rows="4" placeholder="请输入申请原因 ..." /></Input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer" class="btn-width">
        <el-button type="" style="margin-left: 8px" @click="_applyCancel">
          取消
        </el-button>
        <Button type="primary" @click="_applyOk">
          确定
        </Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { consumeRelPurchase } from '../../../../api'

export default {
  components: {},
  data() {
    return {
      formObj: {
        catalogueId: '',
        name: '',
        code: '',
        standardName: ''
      },
      showModal: false,
      showApplyModal: false,
      searchOpen: false,
      applyValue: '',
      sendUserId: '',
      btn: [{ type: 'primary', id: '', name: '导入检测项目' }],
      iconMsg: [{ type: 'pt-a-trash', id: '', name: '删除' }],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      applyFormObj: {
        remark: ''
      },
      applyTitle: '',
      pageColumns: [
        { title: '耗材编号', key: 'chemicalNum', width: 120 },
        { title: '耗材名称', key: 'chemicalName', width: 160 },
        {
          title: '单价',
          key: 'price',
          width: 100
        },
        {
          title: '采购数量',
          key: 'quantity',
          width: 120
        },
        { title: '计量单位', width: 100, key: 'unit' },
        {
          title: '规格',
          key: 'spec',
          width: 200
        },
        {
          title: '等级（纯度）',
          width: 120,
          key: 'quality'
        },
        {
          title: '供应商',
          key: 'supplier',
          width: 200
        },
        {
          title: '制造厂商',
          key: 'manufacturer',
          width: 200
        },
        {
          title: '备注',
          key: 'remark',
          width: 210
        }
      ],
      catalogueId: '',
      getPage: {},
      selectData: []
    }
  },
  methods: {
    _open(catalogueId, label) {
      this.formObj = this.$resetFields(this.formObj)
      this.showModal = true
      this.catalogueId = catalogueId
      this.formObj.catalogueId = catalogueId
      this.getPage.records = []
      this.$refs.pageTable._hideLoading()
      this._page()
    },

    // 操作的序号
    _handleRow(data) {
      this.currentRow = data.row
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },

    _blurSave(key) {
      const data = {
        id: this.currentRow.id,
        obj: {
          [key]: this.currentRow[key]
        }
      }
      this._editRelPurchase(data)
    },

    _editRelPurchase: async function(data) {
      const result = await consumeRelPurchase.edit(data)
      if (result) {
      }
    },

    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await consumeRelPurchase.pageChemicalTemp(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '导入检测项目':
          this.$refs.relItemModal._open(this.catalogueId)
          break
      }
    },
    _importItem() {
      this.$refs.relItemModal._open(this.catalogueId)
    },
    // 操作列操作
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },

    _handleOrderValueEdit(id, value) {
      this._edit({ id: id, obj: { orderBy: value } })
    },

    _edit: async function(data) {
      const result = await consumeRelPurchase.edit(data)
      if (result) {
        this._page()
      }
    },

    _deleteOk: async function(id) {
      const result = await consumeRelPurchase.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    // table结果  返回整行
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this.selectData = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _submitOrderCheck() {
      alert(1)
    },
    _saveOrder(approve) {
      if (this.getPage.records.length === 0) {
        this.$Message.warning('请先添加耗材！')
      } else {
        this.showApplyModal = true
        this.applyValue = approve
        this.applyFormObj.remark = ''
        this.applyTitle = '保存采购单'
      }
    },

    _applyOk() {
      if (undefined === this.applyFormObj.remark) {
        this.$Message.error('表单验证失败!')
        return false
      }

      if (this.applyValue) {
        this._save()
      } else {
        this._submit()
      }
    },

    _save: async function() {
      const data = {
        remark: this.applyFormObj.remark
      }
      // 是否有审批人
      if (this.sendUserId) {
        data.sendUserId = this.sendUserId
      }
      const result = await consumeRelPurchase.savePurOrder(data)
      if (result) {
        this.$Message.success('采购单保存成功！')
      }
    },
    _applyCancel() {
      this.showApplyModal = false
    },
    _cancel() {
      this.showModal = false
    }
  }
}
</script>
