<template>
  <a-card size="small" :bordered="false" class="inventoryCheckingList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="创建时间">
              <a-range-picker
                style="width:100%"
                id="inventoryCheckingList-creatTime"
                :disabledDate="disabledDate"
                @change="creatTimechange"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="状态">
              <v-select code="ENABLED_FLAG" id="inventoryCheckingList-enabledFlag" v-model="queryParam.enabledFlag" allowClear placeholder="请选择状态"></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryCheckingList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="inventoryCheckingList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮 -->
    <div class="table-operator">
      <a-button id="inventoryCheckingList-add" type="primary" class="button-error" @click="handleEdit()">新增</a-button>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :showPagination="false"
      :scroll="{ x: 1480 }"
      bordered>
      <!-- 状态 -->
      <template slot="state" slot-scope="text, record">
        <a-tag :color="record.state==1?'green':'red'" >{{ record.state==1? '待提交': '待单据审核' }}</a-tag>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" @click="handleEexamine(record)" id="inventoryCheckingList-eexamine-btn">审核</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleInventory(record)" id="inventoryCheckingList-inventory-btn">盘点</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleEdit(record)" id="inventoryCheckingList-edit-btn">编辑</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleDetail(record)" id="inventoryCheckingList-detail-btn">详情</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleDel(record)" id="inventoryCheckingList-del-btn">删除</a-button>
      </template>
    </s-table>
    <!-- 新增/编辑盘点单 -->
    <inventory-checking-tips-modal :openModal="openModal" :nowData="nowData" :itemId="itemId" @ok="handleOk" @close="closeModal" />
  </a-card>
</template>

<script>
import { warehouseAllList, warehouseDel } from '@/api/warehouse'
import { STable, VSelect } from '@/components'
import inventoryCheckingTipsModal from './checkingTipsModal.vue'
export default {
  components: { STable, VSelect, inventoryCheckingTipsModal },
  data () {
    return {
      queryParam: { //  查询条件
        enabledFlag: undefined, //  状态
        beginDate: '', //  创建时间  开始时间
        endDate: '' //  创建时间  结束时间
      },
      disabled: false, //  查询、重置按钮是否可操作
      dateFormat: 'YYYY-MM-DD',
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '盘点单号', dataIndex: 'nsso', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存总款数', dataIndex: 'no3', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '库存总数量', dataIndex: 'n4o', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '开单人', dataIndex: 'neo', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单据审核时间', dataIndex: 'shcreateDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', scopedSlots: { customRender: 'state' }, width: 100, align: 'center' },
        { title: '盘点类型', dataIndex: 'sort', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 260, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // return warehouseAllList( this.queryParam ).then(res => {
        //   const data = res.data
        //   this.disabled = false
        //   return data
        // })
        const _this = this
        return new Promise(function (resolve, reject) {
          const data = {
            pageNo: 1,
            pageSize: 10,
            list: [
              { id: '1', state: 1, purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' },
              { id: '2', state: 2, purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' },
              { id: '3', state: 3, purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' },
              { id: '4', state: 4, purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' },
              { id: '5', state: 5, purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' }
            ],
            count: 10
          }
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = i + 1
          }
          _this.disabled = false
          resolve(data)
        })
      },
      openModal: false, //  新增编辑  弹框
      itemId: '', //  当前id
      nowData: null //  当前记录数据
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    //  创建时间  change
    creatTimechange (dates, dateStrings) {
      this.queryParam.beginDate = dateStrings[0]
      this.queryParam.endDate = dateStrings[1]
    },
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemId = row ? row.id : null
      this.nowData = row || null
      this.openModal = true
    },
    //  新增/编辑  成功
    handleOk (type) {
      if (type == 'a') {
        this.$router.push({ path: `/inventoryManagement/inventoryChecking/overall/${this.itemId}` })
      } else if (type == 'b') {
        this.$router.push({ path: `/inventoryManagement/inventoryChecking/selfDisk/${this.itemId}` })
      }
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    //  详情
    handleDetail (row) {
      if (row.state == 3) { //  待盘点  详情
        this.$router.push({ path: `/inventoryManagement/inventoryChecking/makeInventory/detail/${row.id}` })
      } else if (row.state == 4) { //  待财务审核  详情
        this.$router.push({ path: `/inventoryManagement/inventoryChecking/financialAudit/detail/${row.id}` })
      } else { //  其他状态  详情
        this.$router.push({ path: `/inventoryManagement/inventoryChecking/detail/${row.id}` })
      }
    },
    //  审核
    handleEexamine (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要审核通过吗？',
        okText: row.state == 2 ? '确定' : '审核通过',
        cancelText: row.state == 2 ? '取消' : '驳回重盘',
        centered: true,
        onOk () {
          // delectRolePower({
          //   id: row.id
          // }).then(res => {
          //   console.log(res, 'res1111')
          //   if (res.status == 200) {
          //     _this.$message.success(res.message)
          //     _this.$refs.table.refresh()
          //   }
          // })
        }
      })
    },
    //  盘点
    handleInventory (row) {
      this.$router.push({ path: `/inventoryManagement/inventoryChecking/makeInventory/${row.id}` })
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          // warehouseDel({
          //   id: row.id
          // }).then(res => {
          //   if (res.status == 200) {
          //     _this.$message.success(res.message)
          //     _this.$refs.table.refresh()
          //   }
          // })
        }
      })
    }
  }
}
</script>
