<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <notes-search @search="reload"> </notes-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="systemRoleTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>
        <template v-slot:code="{ row }"
          ><el-link type="primary" :underline="false" @click="goDetail(row)">
            {{ row.code }}
          </el-link></template
        >
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="appoint(row)"
          >
            委外
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
			<el-popconfirm
			  class="ele-action"
			  title="确定要撤销此报修记录吗？"
			  @confirm="remove(row)"
			>
			  <template v-slot:reference>
				<el-link type="danger" :underline="false" icon="el-icon-delete">
				  撤销
				</el-link>
			  </template>
			</el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 新建或编辑弹窗 -->
    <AddDialog
      ref="addDialogRef"
      :dialogTitle="addDialogTitle"
      :deviceInfo="deviceInfo"
      :infoData="infoData"
      @refresh="reload"
    />

    <DetailsDialog
      ref="detailsDialogRef"
      :dialogTitle="detailsDialogTitle"
    />
	<!-- 委外弹窗 -->
	<EntrustDialog
	  ref="entDialogRef"	 
	  @refresh="reload"
	/>
  </div>
</template>

<script>
  import { getPage , removeRequest } from '@/api/maintenance/repair_report';
  import NotesSearch from './components/notes-search.vue';
  import AddDialog from './components/addDialog.vue';
  import EntrustDialog from './components/entrustDialog.vue';
  import DetailsDialog from '../components/RepairDetailsDialog.vue';
  import dictMixins from '@/mixins/dictMixins';
  export default {
	mixins: [dictMixins],
    components: {
      NotesSearch,
      AddDialog,
      DetailsDialog,
      EntrustDialog
    },
    data () {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            columnKey: 'code',
            slot: 'code',
            prop: 'code',
            label: '报修记录编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'deviceCode',
            label: '设备编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'deviceName',
            label: '设备名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'type',
            label: '报修类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
			formatter: (_row, _column, cellValue) => {
			  return _row.sourceType==3?'自动报修':'人工报修'
			}
          },
          {
            prop: 'sourceType',
            label: '来源',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
			formatter: (_row, _column, cellValue) => {
			  return this.getDictValue('报修来源', _row.sourceType);
			}
          },
          {
            prop: 'sourceCode',
            label: '来源编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
			formatter: (_row, _column, cellValue) => {
			  return this.getDictValue('报修状态', _row.status);
			}
          },
          {
            prop: 'requestUserName',
            label: '报修人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '报修时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            slot: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            resizable: false,
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        infoData: {},
        repairInfoLogs: [],
        addDialogTitle: '新增报修记录',
        detailsDialogTitle: '报修记录详情',
        deviceInfo: {} //设备信息
      };
    },
    computed: {},
	created () {
	  this.requestDict('报修来源');
	  this.requestDict('报修状态');
	},
    methods: {
      /* 表格数据源 */
      datasource ({ page, limit, where, order }) {
        return getPage({ pageNum: page, size: limit, ...where });
      },
      async changeEnable (row) {
        const res = await putRoles(row);
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '修改成功',
            customClass: 'ele-message-border'
          });
          this.reload();
        }
      },
      /* 刷新表格 */
      reload (where) {
        this.$refs.table.reload({ page: 1, where });
      },

      openEdit (row) {
		  if(row){
			 this.addDialogTitle = '编辑报修记录'; 
		  }else{
			  this.addDialogTitle = '新增报修记录';
		  }
		this.$refs.addDialogRef.init(row)
      },

      goDetail (row) {
        row.tabLabel = '维修信息';
        row.title = '报修记录详情';
        row.workOrderCode = row.id;
        this.$refs.detailsDialogRef.init(row);
        // this.$router.push({
        //   path: '/maintenance/patrol/plan/details',
        //   // query: {
        //   //   id
        //   // }
        // })
      },
	  
	  // 委外
	  appoint(row) {
	    this.$refs.entDialogRef.init(row);
	  },
	  
	  // 撤销
	  remove(row){
		  removeRequest(row.id).then((res) => {
		    this.$message.success('撤销成功！');
		    this.reload();
		  });
	  }
	  
    }
  };
</script>

<style lang="scss" scoped></style>
