<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      v-model:search="query"
      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 #menu="{ row }"> </template>

      <template #heatTreat="scope"> </template>
    </avue-crud>
  </div>
</template>

<script>
// import {
//   getListPersonAbility,
//   removePersonAbility,
//   addPersonAbility,
//   updatePersonAbility,
// } from '@/api/productionSchedulingPlan/scheduling';
export default {
  components: {},
  props: {
    processItem: { type: Object, default: () => {} },
  },
  data() {
    return {
      form: {},
      selectionList: [],
      query: {},
      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,
        menuWidth: 80,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        menu: false,
        header: false,
        column: [
          {
            label: '作业中心',
            prop: 'workCenterName',
            search: false,
            sortable: true,
            span: 12,
            width: 150,
          },
          {
            label: '作业中心',
            prop: 'workCenterId',
            search: true,
            hide: true,
            sortable: true,
            span: 12,
            width: 120,
            searchOrder: 15,
            type: 'select',
            filterable: true,
            dicUrl: '/blade-scheduling/workCenter/findList',
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
            props: {
              label: 'wcName',
              value: 'id',
            },
            change: val => {
              this.onChangeData(val.item, 'workCenterId');
            },
          },

          {
            label: '车间订单号',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 18,
          },
          {
            label: '零件号',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 19,
          },
          {
            label: '零件名称',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '批次号',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 16,
          },
          {
            label: '流程卡号',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 17,
          },
          {
            label: '工艺能力',
            prop: 'craftName',
            search: false,
            sortable: true,
            width: 150,
            span: 12,
          },
          {
            label: '工艺能力',
            prop: 'craftId',
            hide: true,
            search: true,
            sortable: true,
            span: 12,
            width: 120,
            searchOrder: 13,
            type: 'select',
            filterable: true,
            dicUrl: '/blade-scheduling/craftAbility/findList',
            props: {
              label: 'caName',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
            change: val => {
              this.onChangeData(val.item, 'craftId');
            },
          },
          {
            label: '工序',
            prop: 'processName',
            search: false,
            sortable: true,
            width: 150,
            span: 12,
          },
          {
            label: '工序',
            prop: 'processId',
            search: true,
            sortable: true,
            hide: true,
            span: 12,
            searchOrder: 12,

            type: 'select',
            filterable: true,
            dicUrl: '/blade-scheduling/processSet/findList',
            props: {
              label: 'name',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
            change: (val, row) => {
              this.onChangeData(val.item, 'processId');
            },
          },
          {
            label: '加工班组',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 14,
          },
          {
            label: '班组',
            prop: 'jiagongbanzuId',
            search: true,
            sortable: true,
            hide: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 14,
            filterable: true,
            type: 'select',
            dicUrl:'blade-scheduling/teamSet/findList',
            props: {
              label: 'tsName',
              value: 'tsName',
            },

          },
          {
            label: '计划开始时间',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '实际开始时间',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '开工类型',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            type: 'select',
            searchOrder: 11,
            dicData: [
              {
                label: '提前',
                value: '1',
              },
              {
                label: '准时',
                value: '2',
              },
              {
                label: '延迟',
                value: '2',
              },
            ],
          },
          {
            label: '计划完成时间',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '实际完成时间',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '完工类型',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            type: 'select',
            searchOrder: 10,
            dicData: [
              {
                label: '提前',
                value: '1',
              },
              {
                label: '准时',
                value: '2',
              },
              {
                label: '延迟',
                value: '2',
              },
            ],
          },
          {
            label: '计划时长（分钟）',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 180,
          },
          {
            label: '实际时长（分钟）',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 180,
          },
          {
            label: '偏差时长（分钟）',
            prop: 'partName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 180,
          },

          {
            label: '工序完成时间',
            prop: 'cycledate',
            search: true,
            sortable: true,
            hide: true,
            span: 12,
            type: 'date',
            searchLabelWidth: 'auto',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD',
            searchRange: true,
            searchOrder: 20,
          },
          {
            label: '准时偏差（±分钟）',
            prop: 'zhunshipiancha',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            hide: true,
            type: 'number',
            searchOrder: 9,
          },
        ],
      },

      data: [],
    };
  },
  mounted() {
    // 设置默认时间范围为最近七天
    const today = new Date();
    const threeDaysAgo = new Date(today);
    threeDaysAgo.setDate(today.getDate() - 7);

    this.query.cycledate = [this.formatDate(threeDaysAgo), this.formatDate(today)];
  },
  methods: {
    onChangeData() {},
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    rowSave(row, done, loading) {
      // addPersonAbility(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!',
      //     });
      //     done();
      //   },
      //   error => {
      //     window.console.log(error);
      //     loading();
      //   }
      // );
    },
    rowUpdate(row, index, done, loading) {
      // updatePersonAbility(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 removePersonAbility(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();
    },

    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: '操作成功!',
      });
    },

    onLoad(page, params = {}) {
      this.loading = false;
      if (!!params.cycledate) {
        this.query.startTime = params.cycledate[0];
        this.query.endTime = params.cycledate[1];
      }
      // getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
      //   this.data = res.data.data.records;
      //   this.loading = false;
      //   this.page.total = res.data.data.total;
      //   this.selectionClear();
      // });
    },
  },
};
</script>
