<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 #earlyStartCount="scope">
        <el-button type="text" @click="toDetail(scope.row, '提前开工')"
          >{{ scope.row.earlyStartCount }}（{{
            scope.row.earlyStartCountRate == '0' ? '0%' : scope.row.earlyStartCountRate
          }}）</el-button
        >
      </template>
      <template #onTimeStartCount="scope">
        <el-button type="text" @click="toDetail(scope.row, '准时开工')"
          >{{ scope.row.onTimeStartCount }}（{{
            scope.row.onTimeStartCountRate == '0' ? '0%' : scope.row.onTimeStartCountRate
          }}）</el-button
        >
      </template>
      <template #delayStartCount="scope">
        <el-button type="text" @click="toDetail(scope.row, '延迟开工')"
          >{{ scope.row.delayStartCount }}（{{
            scope.row.delayStartCountRate == '0' ? '0%' : scope.row.delayStartCountRate
          }}）</el-button
        >
      </template>
      <template #earlyFinishCount="scope">
        <el-button type="text" @click="toDetail(scope.row, '提前完工')"
          >{{ scope.row.earlyFinishCount }}（{{
            scope.row.earlyFinishCountRate == '0' ? '0%' : scope.row.earlyFinishCountRate
          }}）</el-button
        >
      </template>
      <template #onTimeFinishCount="scope">
        <el-button type="text" @click="toDetail(scope.row, '准时完工')"
          >{{ scope.row.onTimeFinishCount }}（{{
            scope.row.onTimeFinishCountRate == '0' ? '0%' : scope.row.onTimeFinishCountRate
          }}）</el-button
        >
      </template>
      <template #delayFinishCount="scope">
        <el-button type="text" @click="toDetail(scope.row, '延迟完工')"
          >{{ scope.row.delayFinishCount }}（{{
            scope.row.delayFinishCountRate == '0' ? '0%' : scope.row.delayFinishCountRate
          }}）</el-button
        >
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { getProcessExecutePage } from '@/api/productionSchedulingPlan/scheduling';
export default {
  components: {},
  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,
          },
          {
            label: '作业中心',
            prop: 'workCenterId',
            search: true,
            hide: true,
            sortable: true,
            span: 12,
            width: 120,
            searchOrder: 19,
            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: 'craftName',
            search: false,
            sortable: true,
            span: 12,
            searchOrder: 17,
          },
          {
            label: '工艺能力',
            prop: 'caId',
            hide: true,
            search: true,
            sortable: true,
            span: 12,
            width: 120,
            searchOrder: 17,
            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,
            span: 12,
            searchOrder: 16,
          },
          {
            label: '工序',
            prop: 'ppsId',
            search: true,
            sortable: true,
            hide: true,
            span: 12,
            searchOrder: 16,
            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: 'totalCount',
            search: false,
            sortable: true,
            span: 12,
            width: 150,
          },
          {
            label: '准时偏差（±分钟）',
            prop: 'interval',
            search: true,
            sortable: true,
            span: 12,
            searchLabelWidth: 'auto',
            hide: true,
            type: 'number',
          },

          {
            label: '提前开工',
            prop: 'earlyStartCount',
            search: false,
            sortable: true,
            span: 12,
          },
          {
            label: '准时开工',
            prop: 'onTimeStartCount',
            search: false,
            sortable: true,
            span: 12,
          },
          {
            label: '延迟开工',
            prop: 'delayStartCount',
            search: false,
            sortable: true,
            span: 12,
          },
          {
            label: '提前完工',
            prop: 'earlyFinishCount',
            search: false,
            sortable: true,
            span: 12,
          },
          {
            label: '准时完工',
            prop: 'onTimeFinishCount',
            search: false,
            sortable: true,
            span: 12,
          },
          {
            label: '延迟完工',
            prop: 'delayFinishCount',
            search: false,
            sortable: true,
            span: 12,
          },
          {
            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,
          },
        ],
      },

      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)];
    this.query.interval = 30;
    this.onLoad(this.page, this.query);
  },
  methods: {
    onChangeData() {},
    toDetail(row, type) {
      row.interval = this.query.interval;
      row.cycledate = this.query.cycledate;
      this.$emit('toDetail', row, type, '工序');
    },
    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);
    },
    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 = true;
      let query_ = {};
      if (!!params.cycledate) {
        query_.startTime = params.cycledate[0];
        query_.endTime = params.cycledate[1];
      }
      if (!!params.interval) {
        query_.interval = params.interval;
      }
      if (!!params.workCenterId) {
        query_.workCenterId = params.workCenterId;
      }
      if (!!params.caId) {
        query_.caId = params.caId;
      }
      if (!!params.ppsId) {
        query_.ppsId = params.ppsId;
      }
      getProcessExecutePage(page.currentPage, page.pageSize, query_).then(res => {
        this.data = res.data.data.records;
        this.loading = false;
        this.page.total = res.data.data.total;
      });
    },
  },
};
</script>
