<template>
  <basic-container>
    <div class="search_box"></div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <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>

    <!-- 加急 -->
    <!-- <rushDialog :showDialog="isRushOpen" v-if="isRushOpen" @closeDialog="closeDialog"></rushDialog>

         <batchDialog :showDialog="isBatchOpen" v-if="isBatchOpen" @closeDialog="closeDialog"></batchDialog> -->
  </basic-container>
</template>

<script>
import { getList } from '@/api/orderManagement/allocationMessQuery';
import {
  add,
  getRole,
  getRoleAlias,
  getRoleTreeById,
  grant,
  grantTree,
  remove,
  update,
} from '@/api/system/role';
import { mapGetters } from 'vuex';
import { validatenull } from '@/utils/validate';

import rushDialog from './components/rushDialog.vue';
import batchDialog from './components/batchDialog.vue';
export default {
  components: {
    // rushDialog,
    // batchDialog
  },
  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:""
      },
      loading: true,
      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',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        menu: false,
        column: [
          {
            label: '配套单号',
            prop: 'pkno',
            search: true,
            sortable: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入配套单号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '父件号',
            prop: 'prntno',
            search: false,
            sortable: true,
            width: 180,
            span: 12,
            headerAlign: 'center',
            align: 'center',

            rules: [
              {
                required: true,
                message: '请输入父件号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '型号',
            prop: 'mtltmrk',
            search: false,
            sortable: true,
            disabled: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: false,
                message: '请输入型号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '生产标识',
            prop: 'prtlotno',
            search: false,
            sortable: true,
            disabled: true,
            span: 12,
            width: 150,
            headerAlign: 'center',
            align: 'left',
            rules: [
              {
                required: false,
                message: '请输入生产标识',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '需求日期',
            prop: 'pkreqdat',
            search: false,
            sortable: true,
            disabled: true,
            span: 12,
            width: 150,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: false,
                message: '请输入需求日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '父件批次',
            prop: 'fjSplcode',
            search: false,
            sortable: true,
            width: 120,
            display: false,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入父件批次',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '父件配套数',
            prop: 'fjPkavqty',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入父件配套数',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '配套序号',
            prop: 'serialno',
            search: false,
            sortable: true,
            disabled: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入配套序号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '主制部门',
            prop: 'prtmdept',
            search: true,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入主制部门',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划标记',
            prop: 'plnsign',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            disabled: true,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: false,
                message: '请输入计划标记',
                trigger: 'blur',
              },
            ],
          },

          {
            label: '制造订单号',
            prop: 'mono',
            search: false,
            sortable: true,
            width: 150,
            span: 12,
            headerAlign: 'center',
            align: 'center',

            rules: [
              {
                required: true,
                message: '请输入制造订单号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划员',
            prop: 'schemer',
            search: false,
            sortable: true,
            width: 120,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入计划员',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '调度员',
            prop: 'planner1',
            search: false,
            sortable: true,
            searchLabelWidth: 100,
            width: 170,
            span: 12,
            type: 'date',
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入调度员',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '配套日期',
            prop: 'subpkdate',
            search: true,
            sortable: true,
            span: 24,
            type: 'textarea',
            width: 150,
            headerAlign: 'center',
            align: 'left',
            type: 'date',
            searchRange: true,
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
          },
          {
            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;
      }
    },
  },
  methods: {
    // 批量接收
    batchReceiving() {
      this.$confirm('确定将选择数据批量接收?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // return remove(row.id);
        })
        .then(() => {
          // this.onLoad(this.page);
          // this.$message({
          //     type: 'success',
          //     message: '操作成功!',
          // });
        });
    },
    // 批量关闭
    batchClose() {
      this.$confirm('确定将选择数据批量关闭?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // return remove(row.id);
        })
        .then(() => {
          // this.onLoad(this.page);
          // this.$message({
          //     type: 'success',
          //     message: '操作成功!',
          // });
        });
    },
    // 加急
    rushFn() {
      this.isRushOpen = true;
    },
    closeDialog() {
      this.isRushOpen = false;
      this.isBatchOpen = false;
    },
    inBatchesFn(row) {
      this.rowItem = row;
      this.isBatchOpen = true;
    },
    subclass() {
      this.platingSmallOpen = true;
    },
    lookProcessRoute(row) {
      this.isOpen = true;
      this.rowItem = row;
    },
    cancel(refresh) {
      if (refresh) {
        this.$refs.myTable.load();
      }
      this.inBatchesOpen = false;
      this.isOpen = false;
      this.planFormOpen = false;
      this.platingSmallOpen = false;
    },
    initData(roleId) {
      getRoleTreeById(roleId).then(res => {
        const column = this.findObject(this.option.column, 'parentId');
        column.dicData = res.data.data;
      });
    },
    submit() {
      const menuList = this.$refs.treeMenu.getCheckedKeys();
      const dataScopeList = this.$refs.treeDataScope.getCheckedKeys();
      const apiScopeList = this.$refs.treeApiScope.getCheckedKeys();
      grant(this.idsArray, menuList, dataScopeList, apiScopeList).then(() => {
        this.box = false;
        this.$message({
          type: 'success',
          message: '操作成功!',
        });
        this.selectionList = [];
        this.onLoad(this.page);
      });
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    beforeOpen(done, type) {
      if (['add', 'edit'].includes(type)) {
        this.initData(this.form.id);
      }
      if (['edit', 'view'].includes(type)) {
        if (this.form.parentId === '0') {
          this.form.parentId = '';
        }
      }
      getRoleAlias().then(res => {
        const column = this.findObject(this.option.column, 'currentAlias');
        column.dicData = res.data.data;
      });
      done();
    },
    handleRole() {
      if (this.selectionList.length !== 1) {
        this.$message.warning('只能选择一条数据');
        return;
      }
      this.menuTreeObj = [];
      this.dataScopeTreeObj = [];
      this.apiScopeTreeObj = [];
      grantTree().then(res => {
        this.menuGrantList = res.data.data.menu;
        this.dataScopeGrantList = res.data.data.dataScope;
        this.apiScopeGrantList = res.data.data.apiScope;
        getRole(this.ids).then(res => {
          this.menuTreeObj = res.data.data.menu;
          this.dataScopeTreeObj = res.data.data.dataScope;
          this.apiScopeTreeObj = res.data.data.apiScope;
          this.box = true;
        });
      });
    },
    handleRowRole(row) {
      this.menuTreeObj = [];
      this.dataScopeTreeObj = [];
      this.apiScopeTreeObj = [];
      grantTree().then(res => {
        this.menuGrantList = res.data.data.menu;
        this.dataScopeGrantList = res.data.data.dataScope;
        this.apiScopeGrantList = res.data.data.apiScope;
        getRole(row.id).then(res => {
          this.menuTreeObj = res.data.data.menu;
          this.dataScopeTreeObj = res.data.data.dataScope;
          this.apiScopeTreeObj = res.data.data.apiScope;
          this.selectionList.push(row);
          this.box = true;
        });
      });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleChange(file, fileList) {
      // proxy.$Export.xlsx(file.raw).then((data) => {
      //     data.value = data.results;
      // });
      this.$message({
        type: 'success',
        message: '操作成功!',
      });
    },

    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;
          this.selectionClear();
      } catch (err) {
        console.log(err);
      }
      this.loading = false;
    },
  },
  mounted() {},
};
</script>
