<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button @click="addFn()" type="primary">新增任务</el-button>
      </template>

      <template #menu-right="{ size }"> </template>

      <template #menu="scope">
        <el-button type="primary" link @click="viewDetails(scope.row.partId, scope.row)"
          >详情</el-button
        >
        <!-- 0 待分派  1 待接收 2 进行中 3 已完成 -->
        <el-button
          type="primary"
          link
          @click="organization(scope.row.partId, scope.row)"
          v-if="scope.row.taskStatus == 2"
          >编制</el-button
        >
        <el-button
          type="primary"
          link
          @click="receiveFn(scope.row)"
          v-if="scope.row.taskStatus == 1"
          >接收</el-button
        >
        <el-button
          type="primary"
          link
          @click="completedFn(scope.row)"
          v-if="scope.row.taskStatus == 2&& (!scope.row.children || scope.row.children.length === 0)"
          >完成</el-button
        >
        <el-button
          type="primary"
          link
          @click="setBathCrew(scope.row)"
          v-if="scope.row.taskStatus == 1"
          >转派</el-button
        >
      </template>

      <template #bsWorkType.wtName="scope">
        <span>
          {{ scope.row.bsWorkType ? scope.row.bsWorkType.wtName : '-' }}
        </span>
      </template>
      <template #name="{ row }">
        <i :class="row.source" style="margin-right: 5px" />
        <span>{{ row.name }}</span>
      </template>
      <template #source="{ row }">
        <div style="text-align: center">
          <i :class="row.source" />
        </div>
      </template>
    </avue-crud>
    <!-- 转派 -->
    <assignDaialog
      :id="bcId"
      :set-crew-open="setCrewOpen"
      v-if="setCrewOpen"
      type="platingAssort"
      @setCrewOpeSancel="cancel"
      :title="title"
      :updateRow="updateRow"
    ></assignDaialog>

    <!-- 编制 -->
    <dsPartIndex
      :is-open="isOpen"
      v-if="isOpen"
      :part-id="partId"
      @cancel="cancel"
      :partType="partType"
      :dialogType="dialogType"
      :updateRow="updateRow"
    ></dsPartIndex>

    <!-- 返工任务编制 -->
    <reworkDsPartIndex
      :is-open="isReworkOpen"
      v-if="isReworkOpen"
      :part-id="partId"
      @cancel="cancel"
      :partType="partType"
      :dialogType="dialogType"
      :updateRow="updateRow"
    >
    </reworkDsPartIndex>
    <!-- 新增任务 -->
    <addTestDialog v-if="isTestOpen" :showDialog="isTestOpen" @closeDialog="cancel"></addTestDialog>

    <!-- 查看详情数据 -->
    <partDetails
      :is-open="showPartDetails"
      v-if="showPartDetails"
      @cancel="cancel"
      :partType="partType"
      :dialogType="'view'"
      :rowData="detailsRow"
      :detailsType="'taskProcessing'"
      :partId="partId"
    ></partDetails>
  </basic-container>
</template>
<script>
import assignDaialog from './components/assignDaialog.vue';
import dsPartIndex from './components/processMainte/dsPartIndex.vue';
import reworkDsPartIndex from './components/processMainte/reworkDsPartIndex.vue';
import addTestDialog from './components/addTestDialog.vue';
import { getList, completeSet, taskReception } from '@/api/processManagement/taskProcessing';
import partDetails from './components/processMainte/partDetails.vue';
export default {
  components: {
    assignDaialog,
    dsPartIndex,
    addTestDialog,
    reworkDsPartIndex,
    partDetails,
  },
  data() {
    return {
      showPartDetails: false,
      isReworkOpen: false,
      partType: '热表',
      isOpen: false,
      partId: null,
      bcId: null,
      setCrewOpen: false,
      form: {},
      query: {},
      loading: true,
      parentId: '',
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: 'auto',
        calcHeight: 32,
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        editBtnText: '修改',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        viewBtnText: '详情',
        labelWidth: 120,
        menuWidth: 170,
        dialogWidth: 1200,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        showOverflowTooltip: true,
        menuAlign: 'left',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '零件号',
            prop: 'partCode',
            search: true,
            sortable: true,
            overHidden: true,
            width: 210,
            headerAlign: 'center',
            align: 'center',
            searchLabelWidth: 60,
            fixed: 'left',
          },
          {
            label: '零件名称',
            prop: 'partName',
            sortable: true,
            search: false,
            overHidden: true,
            width: 160,
            headerAlign: 'center',
            align: 'left',
          },
          {
            label: '任务类型',
            prop: 'taskType',
            sortable: true,
            search: true,
            overHidden: true,
            type: 'select',
            filterable: true,
            clearable: true,
            headerAlign: 'center',
            align: 'center',
            dicUrl: '/blade-system/dict/dictionary?code=processTaskType',
            props: {
              label: 'dictValue',
              value: 'dictKey',
            },
            width: 120,
          },
          {
            label: 'PDM发布时间',
            prop: 'pdmCreateTime',
            sortable: true,
            search: false,
            overHidden: true,
            width: 180,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: 'PDM工艺路线',
            prop: 'pdmWay',
            sortable: true,
            search: false,
            width: 150,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '工艺路线详情',
            prop: 'wayDetails',
            sortable: true,
            search: false,
            width: 220,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '在制品建议',
            prop: 'wipTake',
            sortable: true,
            search: false,
            overHidden: true,
            width: 150,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '备注',
            prop: 'remarks',
            sortable: true,
            search: false,
            overHidden: true,
            width: 120,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '状态',
            prop: 'taskStatus',
            sortable: true,
            search: true,
            overHidden: true,
            width: 120,
            type: 'select',
            filterable: true,
            clearable: true,
            headerAlign: 'center',
            align: 'center',
            searchLabelWidth: 45,
            dicData: [
              {
                value: 0,
                label: '待分派',
              },
              {
                value: 1,
                label: '待接收',
              },
              {
                value: 2,
                label: '进行中',
              },
              {
                value: 3,
                label: '已完成',
              },
            ],
          },
          {
            label: '分派人',
            prop: 'assignMan',
            sortable: true,
            search: false,
            overHidden: true,
            width: 120,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '分派时间',
            prop: 'assignTime',
            sortable: true,
            search: false,
            overHidden: true,
            width: 180,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '接收人',
            prop: 'craftMan',
            sortable: true,
            search: false,
            overHidden: true,
            width: 120,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '接收时间',
            prop: 'receptionTime',
            sortable: true,
            search: false,
            overHidden: true,
            width: 180,
            headerAlign: 'center',
            align: 'center',
          },
        ],
      },
      data: [],
      title: '分派',
      isTestOpen: false, //新增任务
      detailsRow: {},
      updateRow: [], //
    
    };
  },
  methods: {
   
    // 新增任务
    addFn() {
      this.isTestOpen = true;
    },
    // 处理弹框关闭触发
    cancel(refresh) {
      this.partId = null;
      this.isOpen = false;
      this.showPart = false;
      this.detailOpen = false;
      this.isTestOpen = false;
      this.isReworkOpen = false;
      this.showPartDetails = false;
      this.setCrewOpen = false;
      this.onLoad(this.page);
    },
    // 完成
    completedFn(row) {
      this.$confirm('确认任务是否完成?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        completeSet({ taskIds: row.id }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.onLoad(this.page);
        });
      });
    },
    // 接收
    receiveFn(row) {
      this.$confirm('请确认是否接收任务?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        taskReception({ taskIds: row.id }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.onLoad(this.page);
        });

        // 数据回调进行刷新
        done(row);
      });
    },
    // 查看详情
    viewDetails(partId, row, level) {
      this.showPartDetails = true;
      this.partId = partId;
      this.detailsRow = row;

      this.dialogType = 'view';
      if (row.children && row.children.length >= 0) {
        this.partType = '烧结';
      } else {
        this.partType = '热表';
      }
    },
    // 编制
    organization(partId, row) {
      this.partId = partId;
      this.updateRow = row;
      if (row.taskType == '6') {
        this.isReworkOpen = true;
      } else {
        this.isOpen = true;
      }

      this.dialogType = 'edit';
      if (row.children && row.children.length >= 0) {
        this.partType = '烧结';
      } else {
        this.partType = '热表';
      }
    },
    // 转派
    setBathCrew(row) {
      this.setCrewOpen = true;
      this.updateRow.push(row);
      this.title = '转派';
    },
    // 重置
    searchReset() {
      this.query = {};
      this.parentId = 0;
      this.onLoad(this.page);
    },
    // 搜索
    searchChange(params, done) {
      this.query = params;
      this.parentId = '';
      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);
    },
    // 加载页面数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.data = res.data.data.records;
        this.loading = false;
        this.page.total = this.data.length;
      });
    },
  },
};
</script>
