<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="type">
        <el-select v-model="queryParams.type" placeholder="请选择故障部位" clearable>
          <el-option
            v-for="dict in dict.type.et_fault_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报修进度" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择报修进度" clearable>
          <el-option
            v-for="dict in dict.type.et_fault_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-hasPermi="['system:fault:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="报修id" align="center" prop="faultId" />-->
      <el-table-column label="用户" align="center" prop="userName" />
      <el-table-column label="车辆编号" align="center" prop="sn" />
      <el-table-column label="图片" align="center" prop="picture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="故障部位" align="center" prop="typeStr" />
<!--      <el-table-column label="故障部位" align="center" prop="typeStr">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.et_fault_type" :value="scope.row.type"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="故障详情" align="center" prop="detail" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.et_fault_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="订单id" align="center" prop="orderId" />-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 1"
                     size="mini"
                     type="text"
                     icon="el-icon-view"
                     @click="handleView(scope.row)"
                     v-hasPermi="['system:fault:query']"
          >详情</el-button>
          <el-button v-if="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:fault:query']"
          >审核</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"
    />

    <!-- 添加或修改故障上报对话框 -->
    <el-dialog title="报修详细" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form"  label-width="100px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户：">{{ form.userName }}</el-form-item>
            <el-form-item label="车辆编号：">{{ form.sn }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片：">
              <img :src="form.picture" alt="产品图片" style="max-width: 100%; height: auto;">
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="故障部位：">{{ form.typeStr }}</el-form-item>
            <el-form-item label="添加时间：">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态：">
              <div v-if="form.status == 1">已提交</div>
              <div v-else-if="form.status == 2">已核实</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="故障详情：" >{{ form.detail }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-show="view == true" @click="open = false">关 闭</el-button>
        <el-button v-show="view == false" type="primary" @click="pass">通 过</el-button>
        <el-button v-show="view == false" @click="reject">拒 绝</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFault, getFault, delFault, addFault, updateFault } from "@/api/system/fault";

export default {
  name: "Fault",
  dicts: ['et_fault_status', 'et_fault_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障上报表格数据
      faultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      view: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        type: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
      this.view = true;
    },
    /** 审核 */
    handleEdit(row) {
      this.open = true;
      this.form = row;
      this.view = false;
    },
    /** 查询故障上报列表 */
    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 = {
        faultId: null,
        userId: null,
        type: null,
        createTime: null,
        picture: null,
        orderId: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.faultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加故障上报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const faultId = row.faultId || this.ids
      getFault(faultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改故障上报";
      });
    },
    /** 通过 */
    pass(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.faultId != null) {
            this.form.status = "2";
            console.log("---------"+JSON.stringify(this.form))
            updateFault(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 拒绝 */
    reject(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.faultId != null) {
            this.form.status = "0";
            debugger
            updateFault(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.faultId != null) {
            updateFault(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFault(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const faultIds = row.faultId || this.ids;
      this.$modal.confirm('是否确认删除故障上报编号为"' + faultIds + '"的数据项？').then(function() {
        return delFault(faultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/fault/export', {
        ...this.queryParams
      }, `fault_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
