<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      v-model:search="query"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
    >
      <template #menu-left> </template>
      <template #menu-right> </template>

      <template #poCode="{ row }">
        <el-text type="primary">{{ row.poCode }}</el-text>
      </template>
      <template #prodIdent="{ row }">
        <el-tag>{{ row.prodIdent }}</el-tag>
      </template>
      <template #curStatusText="{ row }">
        <el-tag v-if="row.curStatusText == '已派工'">{{ row.curStatusText }}</el-tag>
        <el-tag class="ml-2" type="success" v-if="row.curStatusText == '已完工'">
          {{ row.curStatusText }}</el-tag
        >
        <el-tag class="ml-2" type="info" v-if="row.curStatusText == '已关闭'">{{
          row.curStatusText
        }}</el-tag>
        <el-tag class="ml-2" type="warning" v-if="row.curStatusText == '加工中'">{{
          row.curStatusText
        }}</el-tag>
        <el-tag class="ml-2" type="danger" v-if="row.curStatusText == '已退回'">{{
          row.curStatusText
        }}</el-tag>
      </template>
      <template #menu="{ row }"> </template>
    </avue-crud>
  </div>
</template>

<script>
import { getList } from '@/api/orderManagement/allocationMessQuery';
import { mapGetters } from 'vuex';
import { validatenull } from '@/utils/validate';
export default {
  components: {},
  props: {
    rowData: {
      type: Object,
      default: () => {},
    },
  },

  data() {
    return {
      inBatchesOpen: false,
      platingSmallOpen: false,
      planOrderArr: [],
      isOpen: false,
      rowItem: {},
      poId: null,
      form: {},
      box: false,
      props: {
        label: 'title',
        value: 'key',
      },
      menuGrantList: [],
      dataScopeGrantList: [],
      apiScopeGrantList: [],
      apiGrantList: [],
      menuTreeObj: [],
      dataScopeTreeObj: [],
      apiScopeTreeObj: [],
      selectionList: [],
      query: {
        pkno: '',
        prtmdept: '',
        subpkdateStart: '',
        subpkdateEnd: '',
        subpkdate: [],
        schemerName: '',
      },
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: 'auto',
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtnText: '修改',
        labelWidth: 120,
        // searchLabelWidth: 120,
        menuWidth: 150,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchLabelWidth: 'auto',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'center',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        menu: false,
        column: [
          {
            label: '配套单号',
            prop: 'pkno',
            search: true,
            sortable: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '父件号',
            prop: 'prntno',
            search: false,
            sortable: true,
            width: 180,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '型号',
            prop: 'mtltmrk',
            search: false,
            sortable: true,
            disabled: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '生产标识',
            prop: 'prtlotno',
            search: false,
            sortable: true,
            disabled: true,
            span: 12,
            width: 150,
            headerAlign: 'center',
            align: 'left',
          },
          {
            label: '需求日期',
            prop: 'pkreqdat',
            search: false,
            sortable: true,
            disabled: true,
            span: 12,
            width: 150,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '父件批次',
            prop: 'fjSplcode',
            search: false,
            sortable: true,
            width: 120,
            display: false,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '父件配套数',
            prop: 'fjPkavqty',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '配套序号',
            prop: 'serialno',
            search: false,
            sortable: true,
            disabled: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '主制部门',
            prop: 'prtmdept',
            search: true,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '计划标记',
            prop: 'plnsign',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            disabled: true,
            headerAlign: 'center',
            align: 'center',
          },

          {
            label: '制造订单号',
            prop: 'mono',
            search: false,
            sortable: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '计划部门',
            prop: 'schemerName',
            search: true,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            searchOrder: 19,
          },
          {
            label: '计划员',
            prop: 'schemer',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '调度员',
            prop: 'planner1',
            search: false,
            sortable: true,
            searchLabelWidth: 100,
            width: 170,
            span: 12,
            type: 'date',
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '配套日期',
            prop: 'subpkdate',
            search: true,
            sortable: true,
            span: 24,
            width: 150,
            headerAlign: 'center',
            align: 'left',
            type: 'date',
            searchRange: true,
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            searchOrder: 20,
            searchRules: [
              {
                required: true,
                message: '请选择配套日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '保管员',
            prop: 'warctlr',
            search: false,
            sortable: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            width: 150,
          },
          {
            label: '条码序号',
            prop: 'barcodeno',
            search: false,
            sortable: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            width: 150,
          },
          {
            label: '计划部门',
            prop: 'plandept',
            search: false,
            sortable: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            width: 150,
          },

          {
            label: '物料标识',
            prop: 'prtclass',
            search: false,
            sortable: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            width: 150,
          },
          {
            label: '备注',
            prop: 'remark',
            search: false,
            sortable: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            width: 150,
          },
        ],
      },

      data: [],
      isRushOpen: false, //加急弹框
      isBatchOpen: false, //分批处理
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.role_add, false),
        viewBtn: this.validData(this.permission.role_view, false),
        delBtn: this.validData(this.permission.role_delete, false),
        editBtn: this.validData(this.permission.role_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
    idsArray() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
  watch: {
    'form.currentAlias'() {
      const alias = this.form.currentAlias;
      if (!validatenull(alias)) {
        this.form.roleAlias = alias;
      }
    },
  },
  mounted() {
    if (Object.keys(this.rowData).length > 0) {
      if (this.rowData.partName) {
        this.query.schemerName = this.rowData.partName;
      }
      if (this.rowData.startDate && this.rowData.endDate) {
        this.query.subpkdate = [this.rowData.startDate, this.rowData.endDate];
      }
      this.$nextTick(() => {
        this.onLoad(this.page, this.query);
      });
    }
  },
  methods: {
    closeDialog() {
      this.isRushOpen = false;
      this.isBatchOpen = false;
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.partCode && Array.isArray(params.partCode)) {
        // 根据后端接口要求，拆分开始时间和结束时间
        // 如果后端接收 startDate 和 endDate
        params.startDate = params.partCode[0];
        params.endDate = params.partCode[1];

        // 删除原始的 partCode 数组，避免传递多余参数或格式错误
        delete params.partCode;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    async onLoad(page, params = {}) {
      // this.loading = true;
      try {
        const res = await getList({
          current: this.page.currentPage,
          size: this.page.pageSize,
          ...this.query,
        });
        this.data = res.data.data.records;
        this.page.total = res.data.data.total;
      } catch (err) {
        console.log(err);
      }
      // this.loading = false;
    },
  },
};
</script>
