<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="90px"
          label-suffix=":"
        >
          <el-form-item label="点位编码">
            <el-select
              v-model="query.point_code"
              clearable
              size="mini"
              placeholder="点位编码"
              class="filter-item"
              filterable
              @remote-method="getPointList"
            >
              <el-option
                v-for="item in pointList"
                :label="item.point_code"
                :value="item.point_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="供应商名称">
            <el-select
              v-model="query.supplierName"
              clearable
              size="mini"
              placeholder="供应商名称"
              class="filter-item"
              filterable
              @remote-method="getSupplierNameList"
            >
              <el-option
                v-for="item in supplierNameList"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料名称">
            <el-select
              v-model="query.productDescription"
              clearable
              size="mini"
              placeholder="物料名称"
              class="filter-item"
              filterable
              @remote-method="getProductDescriptionList"
            >
              <el-option
                v-for="item in productDescriptionList"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="批次">
            <el-select
              v-model="query.ingotBatch"
              clearable
              size="mini"
              placeholder="批次"
              class="filter-item"
              filterable
              @remote-method="getIngotBatchList"
            >
              <el-option
                v-for="item in ingotBatchList"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="工单状态">
            <el-select
              v-model="query.status"
              clearable
              size="mini"
              placeholder="全部"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.work_order_two_status"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="每托根数">
            <el-input-number
              v-model="query.number"
              filterable
              laceholder="每托根数"
              clearable
              controls-position="right"
              :min="0"
              :max="10000"
              :step="10"
              size="mini"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          :disabled="confirm_flag"
          icon="el-icon-check"
          size="mini"
          @click="confirm"
        >
          提交出库
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          :disabled="cancle_flag"
          icon="el-icon-close"
          size="mini"
          @click="cancle"
        >
          取消出库
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        :cell-style="cellStyle"
        style="width: 100%;"
        height="550"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="pointCode" label="点位名称" :min-width="flexWidth('pointCode',crud.data,'点位名称')" />
        <el-table-column prop="regionName" label="区域名称" :min-width="flexWidth('regionName',crud.data,'区域名称')" />
        <el-table-column prop="subTray" label="子托编码" :min-width="flexWidth('subTray',crud.data,'子托编码')" />
        <el-table-column prop="motherTray" label="母托编码" :min-width="flexWidth('motherTray',crud.data,'母托编码')" />
        <!--        <el-table-column prop="somtOrderNo" label="出库单号" :min-width="flexWidth('somtOrderNo',crud.data,'出库单号')" />-->
        <el-table-column prop="moname" label="生产工单" :min-width="flexWidth('moname',crud.data,'生产工单')" />
        <el-table-column prop="status" label="工单状态" :formatter="format_work_order_two_status" :min-width="flexWidth('status',crud.data,'工单状态')" />
        <el-table-column prop="supplierName" label="供应商名称" :min-width="flexWidth('supplierName',crud.data,'供应商名称')" />
        <el-table-column prop="productDescription" label="物料编码" :min-width="flexWidth('productDescription',crud.data,'物料名称')" />
        <el-table-column prop="ingotBatch" label="批次" :min-width="flexWidth('ingotBatch',crud.data,'批次')" />
        <el-table-column prop="siliconGrade" label="棒源等级" :min-width="flexWidth('siliconGrade',crud.data,'棒源等级')" />
        <el-table-column prop="number" label="每托数量(根)" :min-width="flexWidth('number',crud.data,'每托数量(根)')" />
        <el-table-column prop="updateTime" label="入库时间" :min-width="flexWidth('updateTime',crud.data,'入库时间')" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMaterial from './detail'
import CRUD, { crud, header, presenter } 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 crudSchBasePoint from '@/views/wms/sch/point/schBasePoint'

export default {
  name: 'Outdetail',
  dicts: ['work_order_two_status'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '晶棒库出库库存明细',
      url: 'api/report/yl/outdetail',
      idField: 'group_id',
      sort: 'pointCode,desc',
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: false
      },
      query: {
        group_bind_material_status: '2'
      }
    })
  },
  data() {
    return {
      permission: {
      },
      cancle_flag: false,
      confirm_flag: false,
      rows: [],
      pointList: [],
      supplierNameList: [],
      ingotBatchList: [],
      productDescriptionList: [],
      choose: '物料'
    }
  },
  created() {
    this.getPointList()
    this.getSupplierNameList()
    this.getIngotBatchList()
    this.getProductDescriptionList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.handleCurrentChange(null)
      return true
    },
    confirm() {
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选托盘!')
        return
      }

      // 获取选中行的托盘号 subTray
      const palletSNs = this.rows.map(row => row.subTray)
      // 弹出输入回温时间和回温模式的对话框
      this.$prompt('请输入回温模式（0-普通模式，1-快速模式）', '提示', {
        inputPattern: /^[0-1]$/,
        inputErrorMessage: '输入内容必须是 0 或 1',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value: mode }) => {
        // 提交数据给后台接口
        this.$axios.post('/api/workorder/batchChange', {
          pointCodes: palletSNs,
          mode: mode,
          status: '1'
        }).then(response => {
          // 提交成功处理
          this.$message.success('出库提交成功！')
          this.crud.toQuery()
        }).catch(error => {
          // 提交失败处理
          this.$message.error('出库提交失败，请重试')
        })
      })
    },
    cancle() {
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选托盘!')
        return
      }
      // 获取选中行的托盘号 subTray
      const palletSNs = this.rows.map(row => row.subTray)
      // 提交数据给后台接口
      this.$axios.post('/api/workorder/batchChange', {
        pointCodes: palletSNs,
        mode: '0',
        status: '0'
      }).then(response => {
        // 提交成功处理
        this.$message.success('出库取消成功！')
        this.crud.toQuery()
      }).catch(error => {
        // 提交失败处理
        this.$message.error('出库取消失败，请重试')
      })
    },
    handleSelectionChange(val, row) {
      this.rows = this.$refs.table.selection
      var strs = ''
      this.handleCurrentChange(null)
      if (this.rows.length > 0) {
        this.rows.forEach(now => {
          strs += now.status
        })
        if (strs.indexOf('1') != -1) {
          this.confirm_flag = true
        }
        if (strs.indexOf('0') != -1) {
          this.cancle_flag = true
        }
      }
    },
    handleCurrentChange(current) {
      if (current === null) {
        this.cancle_flag = false
        this.confirm_flag = false
      }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      const status = row.status

      if (column.property === 'status') {
        if (status == '1') {
          return 'background: #13ce66'
        } else {
          return 'background: #FFBA00'
        }
      }
    },
    format_work_order_two_status(row, column) {
      return this.dict.label.work_order_two_status[row.status]
    },
    getPointList() { // 获取点位列表
      crudSchBasePoint.getPointsByRegionCodes('YL,YLHC,KJHC').then(res => {
        this.pointList = res
      })
    },
    getSupplierNameList() {
      crudMaterial.getSupplierNameList().then(res => {
        this.supplierNameList = res.content
      })
    },
    getProductDescriptionList() {
      crudMaterial.getProductDescriptionList().then(res => {
        this.productDescriptionList = res.content
      })
    },
    getIngotBatchList() {
      crudMaterial.getIngotBatchList().then(res => {
        this.ingotBatchList = res.content
      })
    }
  }
}
</script>

<style scoped>

</style>
