<template>
  <div>
    <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="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['business:repairForm:add']"
          >新增</el-button>
        </el-col>
        <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>
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="el-icon-download"-->
<!--            size="mini"-->
<!--            @click="handleStencil"-->
<!--            v-hasPermi="['business:repairForm:export']"-->
<!--          >导出上传模板</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <FileUploader-->
<!--            uploadUrl="/business/repairForm/importData "-->
<!--            uploadButtonText="导入"-->
<!--            v-hasPermi="['business:repairForm:import']"-->
<!--            @refreshTable="getList"-->
<!--          />-->
<!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </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" fixed="left" min-width="30px" />
        <el-table-column label="报修单号" align="center" prop="repairNumber" fixed="left" min-width="120px"/>
        <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" min-width="120px" show-overflow-tooltip/>
        <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" min-width="95px"/>
        <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" min-width="70px">
          <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="70px">
          <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="80px">
          <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-edit"
              @click="handleUpdate(scope.row,1)"
              v-hasPermi="['business:repairForm:edit']"
              v-show="scope.row.isDelivery==0&&scope.row.state!=3"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-operation"
              @click="handleDetail(scope.row,2)"
              v-hasPermi="['business:repairForm:edit']"
            >详情</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"
      />

    </div>
    <el-dialog title="检验信息" :visible.sync="jianyanOpen" width="1300px" append-to-body destroy-on-close>
      <examineProject type="2" ref="jianyan" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="jianyanSubmit">确 定</el-button>
        <el-button @click="jianyanOpen=false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="故障信息" :visible.sync="guzhangOpen" width="1300px" append-to-body destroy-on-close>
      <malfunctionProject type="2" ref="guzhang" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="guzhangSubmit">确 定</el-button>
        <el-button @click="guzhangOpen=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { abolishRepairForm, delRepairForm, listRepairForm } from '@/api/business/repairForm'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { formatDate } from '@/utils/index'

import examineProject from '../examineProject/index'
import malfunctionProject from '../malfunctionProject/index'

import repairFormProject from '../repairFormProject/index'

export default {
  name: "RepairForm",
  dicts: ['client_type', 'repair_form_is_delivery','repair_form_state', 'repair_form_settlement_state'],
  components: {
    Treeselect,
    examineProject,
    malfunctionProject,
    repairFormProject
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      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: {
        clientName: [
          { required: true, message: "车辆牌照号不能为空", trigger: "blur" }
        ],
        plateNumber: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", 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" }
        ],
      },
      jianyanOpen: false,
      guzhangOpen: false,
      activeTab: '0',
      tabs:[
        {label:'维修项目',value: '0'},
        {label:'维修材料',value: '1'},
        {label:'竣工信息',value: '2'},
        {label:'交车信息',value: '3'},
        {label:'理赔信息',value: '4'},
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报修单列表 */
    getList() {
      this.loading = true;
      listRepairForm(this.handleTime(this.queryParams,'dengjishijian')).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,
        clientName: undefined,
        phone: undefined,
        clientType: undefined,
        carVin: undefined,
        carTypeCode: undefined,
        carBrand: undefined,
        carModel: undefined,
        carConfiguration: undefined,
        carColour: undefined,
        expendType: undefined,
        chassisNumber: undefined,
        motorNumber: 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: this.$store.state.user.userInfo.userName,
        createTime: formatDate(new Date().getTime()),
        updateBy: undefined,
        updateTime: undefined,
        status: 0,
      };
      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.$router.push({name: "repairFormEdit", query: {repairFormId: undefined, flag: 0, isDelivery: undefined}});
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.$router.push({name: "repairFormEdit", query: {repairFormId: row.id, flag: 2, isDelivery: row.isDelivery}});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({name: "repairFormEdit", query: {repairFormId: row.id, flag: 1, isDelivery: row.isDelivery}});
    },
    /** 删除按钮操作 */
    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
      }, `报修单`)
    },
    /** 模板按钮操作 */
    handleStencil() {
      this.download("business/repairForm/importTemplate", {}, `报修单模板`);
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
        // isDisabled: node.level<4&&!node.children
      };
    },
    jianyanSubmit(){
      if(this.$refs.jianyan.names){
        this.form.examineItem=this.$refs.jianyan.names;
        this.jianyanOpen=false;
      }else{
        this.$message.error('至少选择一项');
      }
    },
    guzhangSubmit(){
      if(this.$refs.guzhang.names){
        this.form.malfunctionItem=this.$refs.guzhang.names;
        this.guzhangOpen=false;
      }else{
        this.$message.error('至少选择一项');
      }
    },
    handleTime(queryParams,name){
      let queryParam={...queryParams}
      if(queryParam[name]&&queryParam[name].length>0){
        queryParam.startTime=queryParam[name][0];
        queryParam.endTime=queryParam[name][1];
        delete queryParam[name]
      }
      return queryParam
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/assets/styles/variables.scss";
.top{
  width: calc(100% - #{$base-sidebar-width});
  height: 50px;
  position: fixed;
  top: 84px;
  right: 0;
  z-index: 99;
  background: #FFF;
  box-shadow: 1px 1px 10px;
  font-size: 16px;
  line-height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: width 0.28s;
}
.top-left{
  display: flex;
  align-items: center;
  margin-top: 10px;
}
.top-left>div{
  padding: 0 20px;
  text-align: center;
}
.top1{
  width: 100%;
  height: 30px;
}
.app-title{
  font-size: 16px;
  font-weight: 600;
  padding: 10px 0;
}
.hideSidebar .top {
  width: calc(100% - 54px);
}

.sidebarHide .top {
  width: 100%;
}

.mobile .top {
  width: 100%;
}
.item{
  width: 100%;
  margin: 20px 0px 0;
  // padding: 0 20px;
  border-radius: 10px;
  box-shadow: 0px 2px 5px #999;
}
.item-title{
  width: 100%;
  height: 50px;
  padding: 0 20px;
  font-size: 18px;
  line-height: 50px;
  border-bottom: 2px dashed #CBDFF1;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.item-main{
  width: calc(100% - 40px);
  padding: 20px 20px 0;
}
.bottom{
  width: calc(100% - #{$base-sidebar-width});
  height: 400px;
  position: fixed;
  bottom: 0px;
  right: 0;
  z-index: 99;
  background: #FFF;
  box-sizing: border-box;
  padding: 0 20px;
}
.hideSidebar .bottom {
  width: calc(100% - 54px);
}

.sidebarHide .bottom {
  width: 100%;
}

.mobile .bottom {
  width: 100%;
}
.bottom1{
  width: 100%;
  height: 400px;
}
</style>
