<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- <patrolSearch @search="search"></patrolSearch> -->
      <seek-page
        :seekList="seekList"
        @search="search"
        :maxLength="4"
        style="margin-bottom: 15px"
      ></seek-page>
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :cache-key="cacheKeyUrl"
        autoAmendPage
      >
        <template v-slot:toolbar>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-plus"
            @click="openEdit('add')"
            >新建临时计划</el-button
          >
        </template>

        <template v-slot:code="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit('detail', row)"
          >
            {{ row.code }}
          </el-link>
        </template>

        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit('edit', row)"
            v-if="row.status != 2 && row.status != 3"
          >
            修改
          </el-link>

          <el-link
            type="primary"
            :underline="false"
            v-if="row.status != 2 && row.status != 3"
            @click="openEdit('dispatch', row)"
          >
            派单
          </el-link>

          <el-popconfirm
            v-if="row.status == 2"
            class="ele-action"
            title="确定要撤回此条数据吗？"
            @confirm="revoke(row.id)"
          >
            <template v-slot:reference>
              <el-link type="primary" :underline="false"> 撤回 </el-link>
            </template>
          </el-popconfirm>

          <el-popconfirm
            class="ele-action"
            title="确定要删除此条数据吗？"
            @confirm="deleteRow(row.id)"
            v-if="row.status != 2 && row.status != 3"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false"> 删除 </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <programRulesDialog
      ref="programRulesDialogRef"
      @reload="reload"
    ></programRulesDialog>
  </div>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import tableColumnsMixin from '@/mixins/tableColumnsMixin';
  import {
    recordrulesplanPage,
    recordrulesplanDelete,
    recordrulesplanRevoke
  } from '@/api/recordRules/index';
  import programRulesDialog from './programRulesDialog.vue';
  import patrolSearch from './patrol-search.vue';

  export default {
    mixins: [dictMixins, tableColumnsMixin],
    components: {
      programRulesDialog,
      patrolSearch
    },
    data() {
      return {
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '计划单号',
            align: 'center',
            minWidth: 160,
            slot: 'code'
          },
          {
            prop: 'name',
            label: '计划名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '记录规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: '',
            label: '记录部门',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              if (row.type == 0) {
                return [
                  ...new Set(
                    (row.executeUser || [])
                      .map((i) => i.groupName)
                      .filter((i) => i)
                  )
                ].join(',');
              }
              return '';
            }
          },
          {
            prop: '',
            label: '负责人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              if (row.type == 0) {
                return row.executeUser.map((i) => i.userName).join(',');
              }
              return '';
            }
          },
          {
            prop: 'executeUser',
            label: '班组',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              if (row.type == 1) {
                return row.executeUser.map((i) => i.teamName).join(',');
              }
              return '';
            }
          },
          {
            prop: 'workshopName',
            label: '记录对象',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          // {
          //   prop: 'autoOrder',
          //   label: '自动派单',
          //   align: 'center',
          //   showOverflowTooltip: true,
          //   minWidth: 110,
          //   formatter: (row) => {
          //     return row.autoOrder ? '是' : '否';
          //   }
          // },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              //  0-未派单，2-执行中，3-已完成，4-已撤回
              const statusMap = {
                0: '未派单',
                2: '执行中',
                3: '已完成',
                4: '已撤回'
              };
              return statusMap[row.status] || '';
            }
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        cacheKeyUrl: 'mes-2511211519-batchrecord-workorder-plan'
      };
    },
    computed: {
      seekList() {
        return [
          {
            label: '计划单号',
            value: 'code',
            type: 'input',
            placeholder: '请输入'
          },
          {
            label: '计划名称',
            value: 'name',
            type: 'input',
            placeholder: '请输入'
          },
          // {
          //   label: '计划配置单号',
          //   value: 'configCode',
          //   type: 'input',
          //   placeholder: '请输入',
          //   labelWidth: 110
          // },
          {
            label: '状态',
            value: 'status',
            type: 'select',
            placeholder: '请输入',
            planList: [
              { value: 0, label: '未派单' },
              { value: 2, label: '执行中' },
              { value: 3, label: '已完成' },
              { value: 4, label: '已撤回' }
            ]
          },
          {
            label: '生成时间',
            value: 'time',
            type: 'date',
            dateType: 'daterange'
          }
        ];
      }
    },
    created() {},
    methods: {
      // 刷新表格
      reload(where = {}) {
        this.$refs.table.reload({
          where
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        // 参数
        const body = {
          ...where,
          ...order,
          pageNum: page,
          size: limit
        };
        return recordrulesplanPage(body);
      },
      search(where) {
        console.log('where', where);
        if (where.time) {
          where.createTimeStart = where.time[0];
          where.createTimeEnd = where.time[1];
          where.time = null;
        }
        this.reload(where);
      },
      openEdit(type, row = null) {
        this.$refs.programRulesDialogRef.open(type, row);
      },
      async deleteRow(id) {
        await recordrulesplanDelete([id]);
        this.$message.success('删除成功');
        this.reload();
      },
      async revoke(id) {
        await recordrulesplanRevoke({ id });
        this.$message.success('撤回成功');
        this.reload();
      }
    }
  };
</script>

<style></style>
