<!-- 盘点记录 -->
<template>
  <div class="container">
    <my-table :data="data" :columns="columns" :table-filter="access === 'admin' ? tableFilter1 : tableFilter2" :table-page="params" @event-handle="eventHandle" @on-change="changePage"></my-table>
    <!-- 采购入库单 -->
    <record-form v-model="show" :data="formData"></record-form>
    <!--  手动发起盘点  -->
    <modal v-model="userHandleShow" width="500px">
      <div class="title" slot="header">
        手动发起盘点
      </div>
      <div class="footer" slot="footer">
        <Button @click="cfm" class="btn" type="primary">提交</Button>
        <Button @click="cancel" class="btn">取消</Button>
      </div>
      <!--  表单主要内容  -->
      <div class="content" ref="print">
        <div class="row">
          <div class="col" style="display: flex;align-items: center">
            <div class="label">选择仓库：</div>
            <div class="value" style="flex: 1">
              <Select v-model="plcStationCode" filterable size="small">
                <Option v-for="(item, index) in selectList" :key="index" :value="item.value">{{ item.label }}</Option>
              </Select>
            </div>
          </div>
        </div>
      </div>
    </modal>
  </div>
</template>

<script>
import { exportExcel } from '@/libs/util'
import { GetCheckRecordPageList, BeginPlcAllStorageCheck } from '@/api/stock'
import { GetStockHouse } from '@/api/baseData'
import RecordForm from './record-form'
import MyTable from '_c/my-table/my-table'
export default {
  name: 'inventory_record',
  components: {
    MyTable,
    RecordForm
  },
  data () {
    return {
      access: this.$store.state.user.access,
      selectList: [],
      userHandleShow: false,
      plcStationCode: '',
      formData: {},
      show: false,
      data: [],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '盘点时间',
          key: 'time',
          minWidth: 150
        },
        {
          title: '盘点类型',
          key: 'type',
          minWidth: 150
        },
        {
          title: '是否异常',
          key: 'isError',
          minWidth: 150,
          render: (h, params) => {
            return h('span', {
              style: {
                color: params.row.isError === '异常' ? 'red' : ''
              }
            // }, params.row.isError)
            }, params.row.isError)
          }
        },
        {
          title: '仓库名称',
          key: 'stockHouseName',
          minWidth: 150
        },
        {
          title: '盘点明细',
          key: 'purchaseBillNo',
          minWidth: 150,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  // _this.formData = {
                  //   time: params.row.time,
                  //   operUser: params.row.operUser,
                  //   isError: params.row.isError,
                  //   type: params.row.type,
                  //   id: params.row.id
                  // }
                  // _this.show = true
                  _this.$router.push({
                    name: 'inventory_record_detail',
                    query: {
                      id: params.row.id
                    }
                  })
                }
              }
            }, '查看详情')
          }
        },
        {
          title: '核对状态',
          key: 'checkState',
          minWidth: 150
          // render: (h, params) => {
          //   return h('span', params.row.isAllCheck ? '已核对' : '')
          // }
        }
      ],
      tableFilter1: [
        {
          name: 'DatePicker',
          type: 'daterange',
          placeholder: '请选择盘点时间'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '手动盘点',
          e: 'userHandle'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'export'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '异常标签查询',
          align: 'right',
          width: '120px',
          e: 'showError'
        }
        // ,
        // {
        //   name: 'RadioGroup',
        //   value: 'type',
        //   list: [
        //     { label: '全部', value: 0 },
        //     { label: '人工盘点', value: 2 },
        //     { label: '每日盘点', value: 1 },
        //     { label: '自动盘点', value: 3 }
        //   ]
        // }
      ],
      tableFilter2: [
        {
          name: 'DatePicker',
          type: 'daterange',
          placeholder: '请选择盘点时间'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'export'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '异常标签查询',
          align: 'right',
          width: '120px',
          e: 'showError'
        }
        // ,
        // {
        //   name: 'RadioGroup',
        //   value: 'type',
        //   list: [
        //     { label: '全部', value: 0 },
        //     { label: '人工盘点', value: 2 },
        //     { label: '每日盘点', value: 1 },
        //     { label: '自动盘点', value: 3 }
        //   ]
        // }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0,
        type: 0
      }
    }
  },
  methods: {
    cancel () {
      this.plcStationCode = ''
      this.userHandleShow = false
    },
    cfm () {
      if (!this.plcStationCode) {
        return this.$Message.error('请选择仓库！')
      }
      BeginPlcAllStorageCheck({
        plcStationCode: this.plcStationCode
      }).then(res => {
        if (res.code === 0) {
          this.$Message.info(res.msg || '操作成功！')
          this.cancel()
        }
      })
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.type = option.type
          this.params.timeStart = option.StartTime
          this.params.timeEnd = option.EndTime
          this.getList()
          break
        case 'export':
          GetCheckRecordPageList({
            ...this.params,
            pageIndex: 1,
            pageSize: 99999
          }).then(res => {
            if (res.code === 0) {
              let data = res.result.list
              if (data.length < 1) return this.$Message.error('数据为空！')
              exportExcel(this.columns, data, '价格维护')
            }
          })
          break
        case 'showError':
          this.$router.push('error_tag')
          break
        case 'userHandle':
          this.userHandleShow = true
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      GetCheckRecordPageList(this.params).then(res => {
        if (res.code === 0) {
          this.data = res.result.list
          this.params.total = res.result.totalCount
        }
      })
    },
    getStockHouse () {
      GetStockHouse().then(res => {
        if (res.code === 0) {
          this.selectList = res.result.map(item => {
            return {
              label: item.name,
              value: item.plcCode
            }
          })
        }
      })
    }
  },
  mounted () {
    this.getList()
    this.getStockHouse()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
</style>
