<template>
  <div>
    <el-dialog
      title="选择销售订单"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :append-to-body="true"
    >
      <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:docNo="{ row }">
            <!-- @click="details(row)" -->
            <el-link
              type="primary"
              @click="pickOrderEdit(row)"
              :underline="false"
            >
              {{ row.docNo }}
            </el-link>
          </template>
          <template v-slot:replied="{ row }">
            {{ row.replied == 1 ? '是' : '否' }}
          </template>
          <template v-slot:reviewStatus="{ row }">
            <span v-if="row.reviewStatus == 0">未提交</span>
            <span v-if="row.reviewStatus == 1">审核中</span>
            <span v-if="row.reviewStatus == 2">审核通过</span>
            <span v-if="row.reviewStatus == 3">审核未通过</span>
          </template>
          <template v-slot:action="{ row }">
            <el-radio class="radio" v-model="radio" :label="row.id"
              ><i></i
            ></el-radio>
          </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>

    <eomEdit ref="eomEditRef" />
  </div>
</template>

<script>
  import { saleordersendrecordPage, getById } from '@/api/mes';
  import eomEdit from './eomEdit.vue';
  export default {
    components: { eomEdit },
    data() {
      return {
        dataSource: {},
        dialogVisible: false,
        pages: {
          pageNum: 1,
          size: 10
        },
        total: 0,
        tableData: [],
        current: {},
        radio: null,
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 80,
            label: '序号',
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'docNo',
            label: '发货编码',
            showOverflowTooltip: true,
            slot: 'docNo'
          },
          {
            prop: 'orderNo',
            label: '销售订单编号',
            showOverflowTooltip: true
          },
          {
            prop: 'contactName',
            label: '客户名称',
            showOverflowTooltip: true
          },
          {
            prop: 'replied',
            label: '是否回执',
            showOverflowTooltip: true,
            slot: 'replied'
          },
          {
            prop: 'reviewStatus',
            label: '状态',
            showOverflowTooltip: true,
            slot: 'reviewStatus'
          },
          {
            prop: 'createTime',
            label: '创建时间',
            showOverflowTooltip: true
          },
          {
            columnKey: 'action',
            slot: 'action',
            align: 'center',
            fixed: 'right',
            width: 50
          }
        ]
      };
    },
    methods: {
      pickOrderEdit(row) {
        this.$refs.eomEditRef.open(row);
      },
      async handleMine() {
        const listParent = this.$parent.warehousingMaterialList;
        if (this.current == null) {
          // if (listParent.length != 0) {
          //   this.$message.warning('当前已选择销售订单，请更换！');
          // } else {
          //   this.$message.warning('请先选择销售单！');
          //   return;
          // }
          if (listParent.length != 0) {
            this.dataSource.then((res) => {
              const data = res.list;
              let arr = [];
              for (const key in listParent) {
                for (const i in data) {
                  if (
                    listParent[key].orderNo == data[i].orderNo &&
                    listParent[key].productId == data[i].id
                  ) {
                    arr.push({ ...data[i] });
                  }
                }
              }
              getById(arr[0].id).then((rep) => {
                const obj = {
                  ...arr[0],
                  tableData: rep.data.productList.map((item) => {
                    return {
                      ...item,
                      productId: arr[0].id,
                      code: item.productCode,
                      name: item.productName,
                      contactCode: arr[0].contactCode
                    };
                  })
                };
                console.log('====', obj);
                this.$emit('success', obj);
                this.dialogVisible = false;
              });
            });
            return;
          } else {
            this.$message.warning('请先选择销售单！');
            return;
          }
        }
        // const current = this.current.orderInfoList;
        // for (const key in current) {
        //   if (
        //     current[key].bomDetailDTOS.length == 0 &&
        //     current[key].instanceList.length == 0
        //   ) {
        //     this.$message.warning('当前领料单数据为空');
        //     return;
        //   }
        // }
        const res = await getById(this.current.id);
        this.current = {
          ...this.current,
          tableData: res.data.productList.map((item) => {
            return {
              ...item,
              productId: this.current.id,
              code: item.productCode,
              name: item.productName,
              contactCode: this.current.contactCode
            };
          })
        };
        console.log('===qq', this.current);
        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;
        const data = saleordersendrecordPage({
          pageNum: page,
          size: limit,
          reviewStatus: 2,
          ...where
        });
        this.dataSource = data;
        return data;
      },
      async open() {
        const listParent = this.$parent.warehousingMaterialList;
        if (listParent.length == 0) {
          this.current = null;
          this.radio = null;
        }
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.table.reload({
            pageNum: 1,
            size: 10,
            where: {}
          });
        });
        // const res = await saleordersendrecordPage(this.pages);
        // console.log(res);
        // this.tableData = res.data.list;
        // this.total = res.data.count;
      },
      chooseRow(row) {
        this.current = row;
        this.radio = row.id;
      },
      handleClose(done) {
        this.dialogVisible = false;
      }
    }
  };
</script>
