<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="故障部位" prop="faultSiteList">
        <el-select v-model="queryParams.faultSiteList" placeholder="请选择故障部位" clearable multiple @change="handleQuery">
          <el-option
            v-for="dict in dict.type.fault_site"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报修进度" prop="appealStatus">
        <el-select v-model="queryParams.appealStatus" placeholder="请选择报修进度" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.appeal_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['bst:fault:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'faultSite'">
              <dict-tag :options="dict.type.fault_site" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'picture'">
              <image-preview
                :src="d.row[column.key]"
                alt="故障图片"
                :width="50" :height="50"
              />
            </template>
            <template v-else-if="column.key === 'appealStatus'">
              <dict-tag :options="dict.type.appeal_status" :value="d.row[column.key]" />
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-has-permi="['bst:fault:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-has-permi="['bst:fault:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看故障详情 -->
    <FaultEditDialog>

    </FaultEditDialog>
  </div>
</template>

<script>
import { listFault, getFault, delFault, addFault, updateFault } from "@/api/bst/fault";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import LocationLog from "@/views/bst/locationLog/index.vue";
import LocationLogViewDialog from "@/views/bst/locationLog/components/LocationLogViewDialog.vue";
import FaultEditDialog from "@/views/bst/fault/components/FaultEditDialog.vue";

  // 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "Fault",
  mixins: [$showColumns],
  components: {FaultEditDialog, FormCol},
  dicts: ['fault_site','appeal_status'],
  data() {
    return {
      span: 24,
      // 字段列表
      columns: [
        //{key: 'id', visible: true, label: '故障申诉id', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'userName', visible: true, label: '用户', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'vehicleCode', visible: true, label: '车辆编码', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'picture', visible: true, label: '图片', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'orderId', visible: true, label: '订单编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'faultSite', visible: true, label: '故障部位', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'faultDetail', visible: true, label: '故障详情', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'appealStatus', visible: true, label: '申诉状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障表格数据
      faultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选中的ID
      selectedId: null,
      defaultSort,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        userName: null,
        vehicleCode: null,
        picture: null,
        orderId: null,
        faultSiteList:[],
        faultDetail: null,
        appealStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        vehicleCode: [
          { required: true, message: "车辆编码不能为空", trigger: "blur" }
        ],
        picture: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        faultSite: [
          { required: true, message: "故障部位不能为空", trigger: "blur" }
        ],
        appealStatus: [
          { required: true, message: "申诉状态:0-已驳回 1-已提交 2-维修中不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询故障列表 */
    getList() {
      this.loading = true;
      listFault(this.queryParams).then(response => {
        this.faultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        vehicleCode: null,
        picture: null,
        orderId: null,
        faultSite: null,
        faultDetail: null,
        appealStatus: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查看详情 */
    handleView(row){
      this.selectedId = row.id
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/fault/export', {
        ...this.queryParams
      }, `fault_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
