<template>
  <div>
    <el-dialog
      title="选择领料单"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="main">
        <ele-pro-table
          ref="table"
          :initLoad="false"
          :columns="columns"
          :current.sync="current"
          highlight-current-row
          :datasource="datasource"
          tool-class="ele-toolbar-form"
          cache-key="systemOrgUserTable"
          @row-click="chooseRow"
        >
          <!-- 表头工具栏 -->
          <template v-slot:code="{ row }">
            <!-- @click="details(row)" -->
            <el-link
              type="primary"
              @click="pickOrderEdit(row)"
              :underline="false"
            >
              {{ row.code }}
            </el-link>
          </template>
          <template v-slot:action="{ row }">
            <el-radio class="radio" v-model="radio" :label="row.id"
              ><i></i
            ></el-radio>
          </template>
          <template v-slot:status="{ row }">
            <span v-if="row.status == 0">未领料</span>
            <span v-else>已领料</span>
          </template>
        </ele-pro-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleMine">确 定</el-button>
      </span>
    </el-dialog>

    <pickOrderEdit ref="pickOrderEditRef" />
  </div>
</template>

<script>
  import { pickOrderPage } from '@/api/mes';
  import pickOrderEdit from './pickOrderEdit.vue';
  export default {
    components: { pickOrderEdit },
    data() {
      return {
        dialogVisible: false,
        pages: {
          pageNum: 1,
          size: 10,
          status: 2
        },
        total: 0,
        tableData: [],
        current: {},
        radio: null,
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 80,
            label: '序号',
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '领料单号',
            showOverflowTooltip: true,
            slot: 'code'
          },
          {
            prop: 'executorName',
            label: '执行人名称',
            showOverflowTooltip: true
          },
          {
            prop: 'executorTime',
            label: '执行日期',
            showOverflowTooltip: true
          },
          {
            prop: 'status',
            label: '领料状态',
            showOverflowTooltip: true,
            slot: 'status'
          },
          {
            columnKey: 'action',
            slot: 'action',
            align: 'center',
            fixed: 'right',
            width: 50
          }
        ]
      };
    },
    methods: {
      pickOrderEdit(row) {
        this.$refs.pickOrderEditRef.open(row);
      },
      handleMine() {
        if (this.current == null) {
          this.$message.warning('请先选择领料单！');
          return;
        }
        const current = this.current.orderInfoList;
        // console.log('current====', current);
        const findCurrent = current.filter(
          (item) =>
            item.bomDetailDTOS.length !== 0 || item.instanceList.length !== 0
        );
        // console.log('findCurrent===', findCurrent);
        if (findCurrent.length == 0) {
          this.$message.warning('当前领料单数据为空');
          return;
        }
        // for (const key in current) {
        //   if (
        //     current[key].bomDetailDTOS.length == 0 &&
        //     current[key].instanceList.length == 0
        //   ) {
        //     this.$message.warning('当前领料单数据为空');
        //     return;
        //   }
        // }
        this.$emit('success', this.current);
        this.dialogVisible = false;
      },
      datasource({ page, limit, where }) {
        // const res = await pickOrderPage(this.pages);
        // this.tableData = res.data.list;
        // this.total = res.data.count;
        return pickOrderPage({
          status: 2,
          pageNum: page,
          size: limit,
          ...where
        });
      },
      async open() {
        const listParent = this.$parent.warehousingMaterialList;
        if (listParent.length == 0) {
          this.current = {};
          this.radio = null;
        }
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.table.reload({
            pageNum: 1,
            size: 10,
            where: {
              status: 2
            }
          });
        });
      },
      chooseRow(row) {
        this.current = row;
        this.radio = row.id;
      },
      handleClose(done) {
        this.dialogVisible = false;
      }
    }
  };
</script>
