<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
        <el-form-item label="报修单号" prop="repairNumber">
          <el-input
            v-model="queryParams.repairNumber"
            placeholder="请输入报修单号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNumber">
          <el-input
            v-model="queryParams.plateNumber"
            placeholder="请输入车牌号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="queryParams.state" clearable placeholder="请选择">
            <el-option v-for="item in dict.type.repair_form_state" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input
            v-model="queryParams.clientName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系方式"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户类型" prop="clientType">
          <el-select v-model="queryParams.clientType" placeholder="请选择">
            <el-option v-for="item in dict.type.client_type" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登记时间" prop="dengjishijian">
          <el-date-picker value-format="yyyy-MM-dd HH:mm:ss"
                          v-model="queryParams.dengjishijian"
                          type="datetimerange"
                          :default-time="['00:00:00', '23:59:59']"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
          </el-date-picker>
        </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="['business:repairForm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairFormList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="报修单号" align="center" prop="repairNumber" />
      <el-table-column label="客户类型" align="center" prop="repairNumber">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.client_type" :value="scope.row.client.type"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="client.name" />
      <el-table-column label="联系方式" align="center" prop="client.phone" min-width="130px" show-overflow-tooltip />
      <el-table-column label="车辆品牌" align="center" prop="car.carBrand" />
      <el-table-column label="车牌号" align="center" prop="car.plateNumber" />
      <el-table-column label="总工时" align="center" prop="totalActualTime" />
      <el-table-column label="工时费用（元）" align="center" prop="totalActualTimeMoney" min-width="120px" />
      <el-table-column label="材料费用（元）" align="center" prop="totalMaterialMoney" min-width="120px" />
      <el-table-column label="拖车费用（元）" align="center" prop="trailerMoney" min-width="120px" />
      <el-table-column label="结算总价（元）" align="center" prop="totalMoney" min-width="120px" />
      <el-table-column label="交工时间" align="center" prop="deliveryTime" min-width="150px" />
      <el-table-column label="结算时间" align="center" prop="settlementTime" min-width="150px" />
      <el-table-column label="登记时间" align="center" prop="createTime" min-width="150px" />
      <el-table-column label="登记人员" align="center" prop="createBy" min-width="100px" />
      <el-table-column label="是否交车" align="center" prop="isDelivery" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_form_is_delivery" :value="scope.row.isDelivery" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" fixed="right" min-width="90px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_form_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="结算状态" align="center" prop="settlementState" fixed="right" min-width="90px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_form_settlement_state" :value="scope.row.settlementState" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="170px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-operation"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:repairForm:abolish']"
              v-show="scope.row.isDelivery==0&&scope.row.state!=3"
            >作废</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="title" :visible.sync="open" width="80%" append-to-body>
      <el-scrollbar style="height: 78vh;">
        <detail :repairFormId="repairFormId" />
      </el-scrollbar>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairForm, getRepairForm, delRepairForm, addRepairForm, updateRepairForm,abolishRepairForm} from "@/api/business/repairForm";
import Detail from '@/views/business/repairForm/detail.vue'

export default {
  name: "RepairForm",
  components: { Detail },
  dicts: ['client_type', 'repair_form_state', 'zhengjianleixing', 'settlement_payment_method', 'repair_form_is_delivery','repair_form_state', 'repair_form_settlement_state'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      repairFormId: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修单表格数据
      repairFormList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairNumber: undefined,
        plateNumber: undefined,
        state: undefined,
        clientName: undefined,
        phone: undefined,
        clientType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        carId: [
          { required: true, message: "车辆ID不能为空", trigger: "blur" }
        ],
        vehicleModelId: [
          { required: true, message: "车型ID不能为空", trigger: "blur" }
        ],
        repairNumber: [
          { required: true, message: "报修单号不能为空", trigger: "blur" }
        ],
        repairType: [
          { required: true, message: "报修单类型不能为空", trigger: "change" }
        ],
        isRebuild: [
          { required: true, message: "是否返修不能为空", trigger: "blur" }
        ],
        repairCause: [
          { required: true, message: "报修原因不能为空", trigger: "blur" }
        ],
        vehicleKilometers: [
          { required: true, message: "公里数不能为空", trigger: "blur" }
        ],
        isOldPart: [
          { required: true, message: "旧件带走不能为空", trigger: "blur" }
        ],
        isByoPart: [
          { required: true, message: "是否自备部件不能为空", trigger: "blur" }
        ],
        dashboardUrl: [
          { required: true, message: "仪表盘照片不能为空", trigger: "blur" }
        ],
        bodyConditionUrl: [
          { required: true, message: "车身状况照片不能为空", trigger: "blur" }
        ],
        trailerDocumentNumber: [
          { required: true, message: "拖车单据编号不能为空", trigger: "blur" }
        ],
        trailerCompany: [
          { required: true, message: "拖车公司不能为空", trigger: "blur" }
        ],
        trailerPlateNumber: [
          { required: true, message: "拖车车牌号不能为空", trigger: "blur" }
        ],
        trailerDriver: [
          { required: true, message: "拖车司机不能为空", trigger: "blur" }
        ],
        trailerPhone: [
          { required: true, message: "拖车联系电话不能为空", trigger: "blur" }
        ],
        trailerVehicleKilometers: [
          { required: true, message: "拖车公里不能为空", trigger: "blur" }
        ],
        trailerMoney: [
          { required: true, message: "拖车费用不能为空", trigger: "blur" }
        ],
        trailerStartTime: [
          { required: true, message: "拖车开始时间不能为空", trigger: "blur" }
        ],
        trailerEndTime: [
          { required: true, message: "拖车结束时间不能为空", trigger: "blur" }
        ],
        repairerCompany: [
          { required: true, message: "承修公司不能为空", trigger: "blur" }
        ],
        repairerDeptId: [
          { required: true, message: "承修部门ID不能为空", trigger: "blur" }
        ],
        repairerGroupId: [
          { required: true, message: "承修班组ID不能为空", trigger: "blur" }
        ],
        malfunctionItem: [
          { required: true, message: "故障项目不能为空", trigger: "blur" }
        ],
        examineItem: [
          { required: true, message: "检验项目不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报修单列表 */
    getList() {
      this.loading = true;
      listRepairForm(this.queryParams).then(response => {
        this.repairFormList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        carId: undefined,
        vehicleModelId: undefined,
        repairNumber: undefined,
        repairType: undefined,
        isRebuild: undefined,
        repairCause: undefined,
        vehicleKilometers: undefined,
        isOldPart: undefined,
        isByoPart: undefined,
        byoPartRemark: undefined,
        byoPartUrl: undefined,
        dashboardUrl: undefined,
        bodyConditionUrl: undefined,
        trailerDocumentNumber: undefined,
        trailerCompany: undefined,
        trailerPlateNumber: undefined,
        trailerDriver: undefined,
        trailerPhone: undefined,
        trailerVehicleKilometers: undefined,
        trailerMoney: undefined,
        trailerStartTime: undefined,
        trailerEndTime: undefined,
        trailerRemark: undefined,
        rescueLocation: undefined,
        destinationLocation: undefined,
        repairerCompany: undefined,
        repairerDeptId: undefined,
        repairerGroupId: undefined,
        malfunctionItem: undefined,
        examineItem: undefined,
        state: undefined,
        maintainEndTime: undefined,
        isDelivery: undefined,
        deliveryTime: undefined,
        settlementTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报修单";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.open = true
      this.title = '报修单详情'
      this.repairFormId = row.id
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getRepairForm(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改报修单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRepairForm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRepairForm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 作废按钮操作 */
      handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认作废报修单号为"' + row.repairNumber + '"的数据项？报修单作废后已出库物料库存将会返回').then(() => {
        this.loading = true;
        return abolishRepairForm(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("作废成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('business/repairForm/export', {
        ...this.queryParams
      }, `维修记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
