<template>
    <div class="order-wraper">
        <div class="search-confirm-wrap">
            <div class="search-wrap">
                <div class="search-item">
                    <div class="search-label">仓库</div>
                    <div class="filter_input_wraper">
                        <el-select v-model="value1" filterable clearable placeholder="请选择">
                            <el-option
                              v-for="item in options1"
                              :key="item.stor_id"
                              :label="item.stor_name"
                              :value="item.stor_id">
                          </el-option>
                        </el-select>
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label search-label_1">日期</div>
                    <div class="filter_input_wraper filter_input_wraper_1">
                        <el-date-picker
                        v-model="date"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                      </el-date-picker>
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">物料</div>
                    <div class="filter_input_wraper">
                        <input type="text" class="filter-input" v-model="material_code">
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">载具号</div>
                    <div class="filter_input_wraper">
                        <input type="text" class="filter-input" v-model="storagevehicle_code">
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">货位号</div>
                    <div class="filter_input_wraper">
                        <input type="text" class="filter-input" v-model="struct_code">
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">盘点位</div>
                    <div class="filter_input_wraper">
                        <el-select v-model="value2" filterable clearable placeholder="请选择">
                          <el-option
                              v-for="item in options2"
                              :key="item.point_code"
                              :label="item.point_name"
                              :value="item.point_code">
                          </el-option>
                        </el-select>
                    </div>
                </div>
                <div class="search-item_3">
                    <button class="button button--primary" @click="_checkGetDtlAll">查询</button>
                    <button class="button button--primary" :disabled="disabled1" :class="{'button--defalut': pkId === '' || value2 === ''}" @click="_checkSendTask">下发</button>
                    <button class="button button--primary" :disabled="disabled2" :class="{'button--defalut': dataList.length === 0}" @click="_checkConfirm">盘点确认</button>
                    <button class="button button--primary" :disabled="disabled3" :class="{'button--defalut': pkId === ''}" @click="_checkConfirmOffer">实盘为准</button>
                    <button class="button button--primary" :disabled="disabled4" :class="{'button--defalut': pkId === ''}" @click="_checkConfirmFinance">账务为准</button>
                </div>
            </div>
        </div>
        <div class="zd_wrapper">
            <div class="grid_wraper">
                <div class="slide_new">
                    <table class="filter-table">
                        <thead>
                            <tr>
                                <th>序号</th>
                                <th>盘点仓位</th>
                                <th>载具号</th>
                                <th>物料号</th>
                                <th>物料规格</th>
                                <th>数量</th>
                                <th>重量(kg)</th>
                                <th>盘点数量</th>
                                <th>是否异常</th>
                                <th>盘点状态</th>
                                <th>盘点站台</th>
                                <th>下发状态</th>
                                <th>单据号</th>
                                <th>业务类型</th>
                                <th>创建人</th>
                                <th>创建时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(e, i) in dataList" :key="i" :class="{'selected_icon': pkId === e.bill_code}" @click="toRadio(e)">
                                <td>{{ i + 1 }}</td>
                                <td>{{e.struct_name}}</td>
                                <td>{{e.storagevehicle_code}}</td>
                                <td>{{e.material_code}}</td>
                                <td>{{e.material_spec}}</td>
                                <td>{{e.qty | numeric(3)}}</td>
                                <td>{{e.base_qty | numeric(3)}}</td>
                                <td><input type="number" v-model="e.fac_qty"></td>
                                <td>{{ ['正常', '盘亏', '盘盈'][Number(e.check_resultI)] }}</td>
                                <td v-if="e.status === '01'">生成</td>
                                <td v-if="e.status === '04'">盘点中</td>
                                <td v-if="e.status === '05'">已盘点</td>
                                <td v-if="e.status === '06'">异常处理中</td>
                                <td v-if="e.status === '07'">异常处理完成</td>
                                <td v-if="e.status === '99'">确认完成</td>
                                <td>{{ e.checkpoint_id }}</td>
                                <td>{{ e.is_down }}</td>
                                <td>{{e.check_code}}</td>
                                <td v-if="e.check_type === '31'">计划盘点</td>
                                <td v-if="e.check_type === '32'">临时盘点</td>
                                <td>{{ e.create_name }}</td>
                                <td>{{ e.create_time }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { checkGetBcpStor, checkGetDtlAll, checkGetPoint, checkSendTask, checkConfirm, checkConfirmOffer, checkConfirmFinance } from '@config/getData2.js'
import {dateTimeFtt} from '@config/utils.js'
export default {
  data () {
    return {
      options1: [],
      value1: '',
      date: [new Date((new Date().getTime() - 6 * 24 * 60 * 60 * 1000)), new Date((new Date().getTime()))],
      options2: [],
      value2: '',
      material_code: '',
      storagevehicle_code: '',
      struct_code: '',
      dataList: [],
      pkId: '',
      pkObj: {},
      disabled1: false,
      disabled2: false,
      disabled3: false,
      disabled4: false
    }
  },
  created () {
    this._checkGetBcpStor()
    this._checkGetDtlAll()
    this._checkGetPoint()
  },
  methods: {
    // 仓库下拉框
    async _checkGetBcpStor () {
      let res = await checkGetBcpStor()
      this.options1 = [...res.data]
    },
    // grid
    async _checkGetDtlAll () {
      let res = await checkGetDtlAll(this.value1, this.date !== null ? dateTimeFtt(this.date[0]) : '', this.date !== null ? dateTimeFtt(this.date[1]) : '', this.material_code, this.storagevehicle_code, this.struct_code)
      this.dataList = [...res.data]
    },
    // 盘点位下拉框
    async _checkGetPoint () {
      let res = await checkGetPoint()
      this.options2 = [...res.data]
    },
    toRadio (e) {
      this.pkId = this.pkId === e.struct_name ? '' : e.struct_name
      this.pkObj = this.pkId === e.struct_name ? e : {}
    },
    // 下发（按钮）
    async _checkSendTask () {
      this.disabled1 = true
      if (!this.pkId || !this.value2) {
        this.disabled1 = false
        return
      }
      try {
        let res = await checkSendTask(this.pkObj, this.value2)
        this.toast(res.message)
        this._checkGetDtlAll()
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    // 盘点确认
    async _checkConfirm () {
      this.disabled2 = true
      let flag = false
      this.dataList.map(el => {
        if (el.fac_qty === '' || Number(el.fac_qty) < 0) {
          flag = true
        }
      })
      if (flag) {
        this.disabled2 = false
        return
      }
      try {
        let res = await checkConfirm(this.dataList)
        this.toast(res.message)
        this._checkGetDtlAll()
        this.disabled2 = false
      } catch (e) {
        this.disabled2 = false
      }
    },
    // 实盘为准
    async _checkConfirmOffer () {
      this.disabled3 = true
      if (!this.pkId) {
        this.disabled3 = false
        return
      }
      try {
        let res = await checkConfirmOffer(this.pkObj)
        this.toast(res.message)
        this._checkGetDtlAll()
        this.disabled3 = false
      } catch (e) {
        this.disabled3 = false
      }
    },
    // 财务为准
    async _checkConfirmFinance () {
      this.disabled4 = true
      if (!this.pkId) {
        this.disabled4 = false
        return
      }
      try {
        let res = await checkConfirmFinance(this.pkObj)
        this.toast(res.message)
        this._checkGetDtlAll()
        this.disabled4 = false
      } catch (e) {
        this.disabled4 = false
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.zd_wrapper
    height calc(100% - 135px)
.filter_input_wraper_1
    width calc(100% - 45px)
.search-label_1
    width 45px
</style>
