<template>
  <div>
    <Modal v-model="showModal" title="采购单列表" width="100" class="full-screen">
      <!--内容-->
      <div>
        <Row>
          <!--查询-->
          <Col span="24" />
          <Form inline onsubmit="return false" :label-width="70">
            <Form-item label="申请编号:" class="search-item">
              <Input v-model="formObj.num" placeholder="请输入申请编号" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item label="申请原因:" class="search-item">
              <Input v-model="formObj.remark" placeholder="请输入申请原因" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item label="申请人:" class="search-item">
              <Input v-model="formObj.applyer" placeholder="请输入申请人" @on-enter="_formSearch" /></Input>
            </Form-item>
            <Form-item label="申请时间:" class="search-item">
              <DatePicker
                :value="dateValue"
                format="yyyy-MM-dd"
                type="daterange"
                placement="bottom-end"
                placeholder="请选择申请时间"
                style="width: 100%"
                @on-change="_dateChange"
              />
            </Form-item>
            <Form-item class="search-btn">
              <Button type="primary" @click="_formSearch">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <Col span="24" />
          <element-table
            ref="pageTable"
            :page-columns="pageColumns"
            :table-height="tableHeight"
            :get-page="getPage"
            click="true"
            :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.date">
                  {{ scope.row[item.key]?$dateformat(scope.row[item.key], 'yyyy-mm-dd HH:MM:ss'):'' }}
                </div>
                <div v-else-if="item.status">
                  <span v-if="scope.row[item.key]===0" class="blue-color">
                    {{ '待提交' }}
                  </span>
                  <span v-else-if="scope.row[item.key]===1" class="blue-color">
                    {{ '审批中' }}
                  </span>
                  <span v-else-if="scope.row[item.key]===3" class="blue-color">
                    {{ '待采购' }}
                  </span>
                  <span v-else-if="scope.row[item.key]===2" class="blue-color">
                    {{ '审批未通过' }}
                  </span>
                  <span v-else-if="scope.row[item.key]===5" class="blue-color">
                    {{ '审批驳回' }}
                  </span>
                  <span v-else-if="scope.row[item.key]===4" class="blue-color">
                    {{ '完成' }}
                  </span>
                  <span v-else-if="scope.row[item.key]===6" class="blue-color">
                    {{ '采购中' }}
                  </span>
                </div>
                <div v-else>
                  {{ scope.row[item.key] }}
                </div>
              </template>
            </el-table-column>
          </element-table>
          </Col>
          <Col span="24" />
          <LmsChemicalPurchaseOrderInStock
            ref="bottomModal"
            @on-result-change="_bottomResult"
          />
          </Col>
        </Row>
      </div>
    </Modal>
  </div>
</template>
<script>
import { consumePurchaseOrder } from '../../../../api'
import LmsChemicalPurchaseOrderInStock from './BuyOrderDetail'
export default {
  components: {
    LmsChemicalPurchaseOrderInStock
  },
  data() {
    return {
      showModal: false,
      formObj: {
        num: '',
        remark: '',
        applyer: '',
        beginDate: '',
        endDate: ''
      },
      dateValue: [],
      pageColumns: [
        { title: '采购申请单号', key: 'num', width: 150 },
        { title: '状态', key: 'status', width: 150, status: true },
        { title: '申请原因', key: 'remark', width: 200 },
        { title: '创建人', key: 'applyer', width: 150 },
        { title: '创建时间', key: 'ctime', width: 160, date: true }
      ],
      getPage: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search') - 300
      } else {
        return this.$tableHeight('noSearch') - 300
      }
    }
  },
  mounted() {},
  methods: {
    _openPurchaseOrder() {
      this.showModal = true
      this.formObj = this.$resetFields(this.formObj)
      this.dateValue = []
      this._page()
      this.$refs.bottomModal._open()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.getPage = data.page
          break
        case 'click':
          this.$refs.bottomModal._open(data)
          break
        default:
          this._page()
      }
    },
    _dateChange(data) {
      if (data[0] === data[1]) {
        // 开始和结束为同一天
        this.formObj.beginDate = data[0]
        this.formObj.endDate = data[0]
      } else {
        this.formObj.beginDate = data[0]
        this.formObj.endDate = data[1]
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await consumePurchaseOrder.pageWaitPurchase(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this._page()
      // this.$refs.pageTable._pageChange(1)
      //  刷新时 下面两个同时清空数据
      this.$refs.bottomModal._clearPage()
    },
    _bottomResult() {
      this._page()
      // this.$refs.bottomModal._clearPage()
    }
  }
}
</script>
