<template>


  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
<!--        <label class="el-form-item-label">计划号</label>-->
        <el-input v-model="query.planNumber" clearable placeholder="计划号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.bigItemCode" clearable placeholder="机型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.bigItemName" clearable placeholder="机型名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.workAreaWorkingStation" clearable placeholder="指示纳所" style="width: 150px;" class="filter-item" filterable
                   @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in workAreaOptions"
            :key="item.id"
            :label="item.code"
            :value="item.code">
            <span style="float: left;width: 60px" >{{ item.code }}</span>
            <span style="width: 200px; color: #8492a6; font-size: 13px">{{ item.name }}</span>
          </el-option>
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          size="mini"
          v-permission="permission.audit"
          :disabled="crud.selections.length === 0"
          @click="auditM(crud.selections)"
        >
          审核
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          size="mini"
          v-permission="permission.qxAudit"
          :disabled="crud.selections.length === 0"
          @click="qxAuditM(crud.selections[0])"
        >
          审核取消
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <el-form-item label="完成品" prop="bigItem">
<!--            <el-input v-model="form.bigItem" style="width: 350px;"/>-->
            <el-select v-model="form.bigItem" value-key="id" style="width:100%"
                       placeholder="完成品" clearable filterable
            >
              <el-option
                v-for="item in bigItems"
                :key="item.id"
                :label="item.code"
                :value="item"
              >
                <span style="float: left;width: 110px" >{{item.code }}</span>
                <span style="width: 200px; color: #8492a6; font-size: 13px">{{item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="数量" prop="orderQty">
            <el-input v-model="form.orderQty" style="width:100%"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" :height="crud.tableHeight" size="small"
                style="width: 100%;" @selection-change="crud.selectionChangeHandler" border
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="序号"/>
        <el-table-column prop="planNumber" label="计划号"/>
        <el-table-column prop="workArea.gcCode" label="接收工厂" width="80"/>
        <el-table-column prop="workArea.code" label="工位" width="80"/>
        <el-table-column prop="workArea.name" label="指示纳所" width="200"/>
        <el-table-column prop="planDate" label="计划日期">
          <template slot-scope="scope">
            {{getFormatDate(scope.row.planDate)}}
          </template>
        </el-table-column>
        <el-table-column prop="bigItem.code" label="机型"/>
        <el-table-column prop="bigItem.name" label="机型名称"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.pick_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"/>
        <el-table-column prop="orderQty" label="订单数量"/>
        <el-table-column prop="allocatedQty" label="出单数量"/>
        <el-table-column v-if="checkPer(['admin','pickKitPlan:edit','pickKitPlan:del'])" label="操作" width="150px"
                         align="center" fixed="right"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudPickKitPlan, { audit, queryPickKitPlanList, qxAudit } from '@/api/pickKitPlan'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { queryAreaList } from '@/api/area'
import { queryBigItemAll } from '@/api/bigItem'
import { formatDate } from '@/utils/commonUtils'

const defaultForm = {
  id: null,
  planNumber: null,
  workArea: null,
  planDate: null,
  bigItem: null,
  status: "WCD",
  remark: null,
  orderQty: 0,
  allocatedQty: 0,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'PickKitPlan',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status','station_type'],
  cruds() {
    return CRUD({
      title: '成套计划',
      url: 'api/pickKitPlan',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudPickKitPlan }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'pickKitPlan:add'],
        edit: ['admin', 'pickKitPlan:edit'],
        del: ['admin', 'pickKitPlan:del'],
        audit: ['admin', 'pickKitPlan:audit'],
        qxAudit: ['admin', 'pickKitPlan:qxAudit']
      },
      rules: {
        workArea: [
          { required: true, message: '工位不能为空', trigger: 'blur' }
        ],
        bigItem: [
          { required: true, message: 'Bom完成品工位清单不能为空', trigger: 'blur' }
        ],
        orderQty: [
          { required: true, message: '订单数量不能为空', trigger: 'blur' }
        ]
      },
      //工位
      workAreaOptions: [],
      //未出单的单号
      planNumberOptions:[],
      //BOM清单
      bigItems: [],
      selectIds: {
        ids: []
      },
      bigItemName: null,
      // auditOnOff:false
    }
  },
  watch:{
    // auditOnOff: {
    //   handler(newName, oldName) {
    //     this.auditOnOff = newName
    //   },
    //   immediate: true,
    //   deep: true
    // }
  },
  mounted() {
    this.initData()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    initData(){
      //工位
      queryAreaList({bexb:true,enabled:true}).then(res=>{
        this.workAreaOptions = res
      })
      queryBigItemAll().then(res => {
          this.bigItems = res
        })
      //
      queryPickKitPlanList({status: "WCD"}).then(res=>{
        this.planNumberOptions = res;
      })
    },
    //过滤重复数据
    filterListObj(listObj) {
      let date = []
      let onOff = true
      listObj.filter((i) => {
        onOff = true
        date.filter((ii) => {
          if (i.id === ii.id) {
            onOff = false
          }
        })
        if (onOff) {
          date.push(i)
        }
      })
      return date
    },
    //审核
    auditM(data){
      let data_ = this.selectIds;
      console.log(data_)
      data_.ids = data.map(v => v.id);
      audit(data_.ids).then(res=>{
        this.crud.toQuery()
        this.$message.success("审核成功");
      })
    },
    //取消审核
    qxAuditM(data){
      if (data.status !== 'YCD'){
        this.$message.error("该单未审核");
        return
      }
      qxAudit(data.id).then(res=>{
        this.crud.toQuery()
        this.$message.success("取消审核成功");
      })
    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
  }
}
</script>

<style scoped>

</style>
