<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"
    >
      <template #menu-left> </template>
      <template #menu-right> </template>
      <template #menu="{ row }"> </template>

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

<script>
import { getProcessExecuteDetailPage } 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: 'woCode',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 18,
          },
          {
            label: '零件号',
            prop: 'partCode',
            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: 'batchNo',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 16,
          },
          {
            label: '流程卡号',
            prop: 'cardNo',
            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: 'caId',
            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: 'ppsId',
            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: 'teamName',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            searchOrder: 14,
          },
          {
            label: '班组',
            prop: 'makeTeam',
            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: 'planStartTime',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '实际开始时间',
            prop: 'factStartTime',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '开工类型',
            prop: 'startType',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            type: 'select',
            searchOrder: 11,
            dicData: [
              {
                label: '提前',
                value: '提前',
              },
              {
                label: '准时',
                value: '准时',
              },
              {
                label: '延迟',
                value: '延迟',
              },
            ],
          },
          {
            label: '计划完成时间',
            prop: 'planEndTime',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '实际完成时间',
            prop: 'factEndTime',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
          },
          {
            label: '完工类型',
            prop: 'endType',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 150,
            type: 'select',
            searchOrder: 10,
            dicData: [
              {
                label: '提前',
                value: '提前',
              },
              {
                label: '准时',
                value: '准时',
              },
              {
                label: '延迟',
                value: '延迟',
              },
            ],
          },
          {
            label: '计划时长（分钟）',
            prop: 'planDurationMinute',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 180,
          },
          {
            label: '实际时长（分钟）',
            prop: 'factDurationMinute',
            search: false,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            width: 180,
          },
          {
            label: '偏差时长（分钟）',
            prop: 'biasDurationMinute',
            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: 'interval',
            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)];

    console.log(this.processItem, 'processItem');
    if (this.processItem.cycledate) {
      this.query.cycledate = this.processItem.cycledate;
    }
    if (this.processItem.interval) {
      this.query.interval = this.processItem.interval;
    }
    if (this.processItem.workCenterId) {
      this.query.workCenterId = this.processItem.workCenterId + '';
    }
    if (this.processItem.caId) {
      this.query.caId = this.processItem.caId + '';
    }
    if (this.processItem.ppsId) {
      this.query.ppsId = this.processItem.ppsId + '';
    }
    if (this.processItem.type) {
      if (this.processItem.type == '提前开工') {
        this.query.startType = '提前';
      }
      if (this.processItem.type == '准时开工') {
        this.query.startType = '准时';
      }
      if (this.processItem.type == '延迟开工') {
        this.query.startType = '延迟';
      }
      if (this.processItem.type == '提前完工') {
        this.query.endType = '提前';
      }
      if (this.processItem.type == '准时完工') {
        this.query.endType = '准时';
      }
      if (this.processItem.type == '延迟完工') {
        this.query.endType = '延迟';
      }
    }
    this.onLoad(this.page,this.query);
  },
  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.query.interval = 30
      this.onLoad(this.page,this.query);
    },
    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;
      this.onLoad(this.page, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page, this.query);
    },
    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;
      let query_ = {};
      if (!!this.query.cycledate) {
        query_.startTime = this.query.cycledate[0];
        query_.endTime = this.query.cycledate[1];
      }
      if (!!this.query.interval) {
        query_.interval = this.query.interval;
      }
      if (!!this.query.workCenterId) {
        query_.workCenterId = this.query.workCenterId;
      }
      if (!!this.query.cardNo) {
        query_.cardNo = this.query.cardNo;
      }
      if (!!this.query.batchNo) {
        query_.batchNo = this.query.batchNo;
      }
      if (!!this.query.woCode) {
        query_.woCode = this.query.woCode;
      }
      if (!!this.query.startType) {
        query_.startType = this.query.startType;
      }
      if (!!this.query.endType) {
        query_.endType = this.query.endType;
      }
      if (!!this.query.caId) {
        query_.caId = this.query.caId;
      }
      if (!!this.query.ppsId) {
        query_.ppsId = this.query.ppsId;
      }
      if (!!this.query.partCode) {
        query_.partCode = this.query.partCode;
      }
      if (!!this.query.makeTeam) {
        query_.makeTeam = this.query.makeTeam;
      }
      getProcessExecuteDetailPage(page.currentPage, page.pageSize, query_).then(res => {
        this.data = res.data.data.records;
        this.loading = false;
        this.page.total = res.data.data.total;
      });
    },
  },
};
</script>
