<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="结算单号" prop="settlementNumber">
        <el-input
          v-model="queryParams.settlementNumber"
          placeholder="请输入结算单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="clientPhone">
        <el-input
          v-model="queryParams.clientPhone"
          placeholder="请输入客户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择结算状态" style="width: 100%;">
          <el-option v-for="item in dict.type.settlement_state" :key="item.value" :value="item.value" :label="item.label" />
        </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="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:settlementMaintain:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:settlementMaintain:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:settlementMaintain:remove']"-->
<!--        >删除</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:settlementMaintain:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settlementMaintainList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="结算单号" align="center" prop="settlementNumber" />
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="客户手机号" align="center" prop="clientPhone" show-overflow-tooltip />
<!--      <el-table-column label="客户类型" align="center" prop="clientType" />-->
      <el-table-column label="支付方式" align="center" prop="paymentMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_payment_method" :value="scope.row.paymentMethod" />
        </template>
      </el-table-column>
      <el-table-column label="证件类型" align="center" prop="clientCertificateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhengjianleixing" :value="scope.row.clientCertificateType" />
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center" prop="clientCertificateNumber" />
      <el-table-column label="工时费优惠" align="center" prop="manHourDiscountMoney" />
      <el-table-column label="原料费优惠" align="center" prop="materialDiscountMoney" />
      <el-table-column label="结算时间" align="center" prop="settlementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="付款账号" align="center" prop="paymentAccount" />-->
<!--      <el-table-column label="账号名称" align="center" prop="paymentAccountName" />-->
      <el-table-column label="付款凭证" align="center" prop="paymentVoucher">
        <template slot-scope="scope">
          <image-preview :src="scope.row.paymentVoucher" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="收款时间" align="center" prop="collectionTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.collectionTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="收款回执" align="center" prop="collectionVoucher" />-->
<!--      <el-table-column label="收款账号" align="center" prop="collectionAccount" />-->
<!--      <el-table-column label="收款流水号" align="center" prop="collectionSerialNumber" />-->
<!--      <el-table-column label="收款备注" align="center" prop="collectionRemark" />-->
      <el-table-column label="结算状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="结算单备注" align="center" prop="settlementRemark" />
      <el-table-column label="操作" align="center" width="100px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:settlementMaintain:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="scope.row.state === 2"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:settlementMaintain: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"
    />

    <!-- 添加或修改结算维修单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="结算单号" prop="settlementNumber">
          <el-input v-model="form.settlementNumber" placeholder="请输入结算单号" />
        </el-form-item>
        <el-form-item label="客户ID" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户ID" />
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户手机号" prop="clientPhone">
          <el-input v-model="form.clientPhone" placeholder="请输入客户手机号" />
        </el-form-item>
        <el-form-item label="证件号" prop="clientCertificateNumber">
          <el-input v-model="form.clientCertificateNumber" placeholder="请输入证件号" />
        </el-form-item>
        <el-form-item label="工时费优惠" prop="manHourDiscountMoney">
          <el-input v-model="form.manHourDiscountMoney" placeholder="请输入工时费优惠" />
        </el-form-item>
        <el-form-item label="原料费优惠" prop="materialDiscountMoney">
          <el-input v-model="form.materialDiscountMoney" placeholder="请输入原料费优惠" />
        </el-form-item>
        <el-form-item label="结算时间" prop="settlementTime">
          <el-date-picker clearable
            v-model="form.settlementTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结算时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="付款账号" prop="paymentAccount">
          <el-input v-model="form.paymentAccount" placeholder="请输入付款账号" />
        </el-form-item>
        <el-form-item label="付款账号名称" prop="paymentAccountName">
          <el-input v-model="form.paymentAccountName" placeholder="请输入付款账号名称" />
        </el-form-item>
        <el-form-item label="付款凭证" prop="paymentVoucher">
          <el-input v-model="form.paymentVoucher" placeholder="请输入付款凭证" />
        </el-form-item>
        <el-form-item label="结算单备注" prop="settlementRemark">
          <el-input v-model="form.settlementRemark" placeholder="请输入结算单备注" />
        </el-form-item>
        <el-form-item label="收款时间" prop="collectionTime">
          <el-date-picker clearable
            v-model="form.collectionTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择收款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收款回执" prop="collectionVoucher">
          <el-input v-model="form.collectionVoucher" placeholder="请输入收款回执" />
        </el-form-item>
        <el-form-item label="收款账号" prop="collectionAccount">
          <el-input v-model="form.collectionAccount" placeholder="请输入收款账号" />
        </el-form-item>
        <el-form-item label="收款流水号" prop="collectionSerialNumber">
          <el-input v-model="form.collectionSerialNumber" placeholder="请输入收款流水号" />
        </el-form-item>
        <el-form-item label="收款备注" prop="collectionRemark">
          <el-input v-model="form.collectionRemark" placeholder="请输入收款备注" />
        </el-form-item>
        <el-form-item label="结算状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入结算状态" />
        </el-form-item>
        <el-form-item label="创建部门" prop="createDeptId">
          <el-input v-model="form.createDeptId" placeholder="请输入创建部门" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="80%" append-to-body>
      <span style="font-weight: bold; font-size: 18px">结算信息</span>
      <el-divider />
      <el-descriptions border :column="4">
        <el-descriptions-item label="客户名称">{{form.clientName}}</el-descriptions-item>
        <el-descriptions-item label="证件类型">
          <dict-tag :options="dict.type.zhengjianleixing" :value="form.clientCertificateType" />
        </el-descriptions-item>
        <el-descriptions-item label="证件号">{{form.clientCertificateNumber}}</el-descriptions-item>
        <el-descriptions-item label="结算单号">{{form.settlementNumber}}</el-descriptions-item>
        <el-descriptions-item label="支付方式">
          <dict-tag :options="dict.type.settlement_payment_method" :value="form.paymentMethod" />
        </el-descriptions-item>
        <el-descriptions-item label="结算时间">{{form.settlementTime}}</el-descriptions-item>
        <el-descriptions-item label="付款凭证">
          <image-preview :src="form.paymentVoucher" :width="50" :height="50"/>
        </el-descriptions-item>
        <el-descriptions-item label="结算单备注">{{form.settlementRemark}}</el-descriptions-item>
        <el-descriptions-item label="客户手机号">{{form.clientPhone}}</el-descriptions-item>
      </el-descriptions>
      <span style="font-weight: bold; font-size: 18px">报修信息</span>
      <el-divider />
      <el-table :data="form.repairFormList">
        <!--          <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">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.repair_form_state" :value="scope.row.state" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listSettlementMaintain, getSettlementMaintain, delSettlementMaintain, addSettlementMaintain, updateSettlementMaintain } from "@/api/business/settlementMaintain";

export default {
  name: "SettlementMaintain",
  dicts:['settlement_state', 'zhengjianleixing', 'client_type', 'settlement_payment_method', 'repair_form_is_delivery', 'repair_form_state'],
  props: {
    clientType:{}
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算维修单表格数据
      settlementMaintainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settlementNumber: undefined,
        clientName: undefined,
        clientPhone: undefined,
        state: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientId: [
          { required: true, message: "客户ID不能为空", trigger: "blur" }
        ],
        clientName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        clientPhone: [
          { required: true, message: "客户手机号不能为空", trigger: "blur" }
        ],
        clientType: [
          { required: true, message: "客户类型不能为空", trigger: "change" }
        ],
        clientCertificateType: [
          { required: true, message: "证件类型不能为空", trigger: "change" }
        ],
        clientCertificateNumber: [
          { required: true, message: "证件号不能为空", trigger: "blur" }
        ],
        manHourDiscountMoney: [
          { required: true, message: "工时费优惠不能为空", trigger: "blur" }
        ],
        materialDiscountMoney: [
          { required: true, message: "原料费优惠不能为空", trigger: "blur" }
        ],
        settlementTime: [
          { required: true, message: "结算时间不能为空", trigger: "blur" }
        ],
        paymentMethod: [
          { required: true, message: "支付方式不能为空", trigger: "blur" }
        ],
        paymentAccount: [
          { required: true, message: "付款账号不能为空", trigger: "blur" }
        ],
        paymentAccountName: [
          { required: true, message: "付款账号名称不能为空", trigger: "blur" }
        ],
        paymentVoucher: [
          { required: true, message: "付款凭证不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询结算维修单列表 */
    getList() {
      this.loading = true;
      this.queryParams.clientType = this.clientType
      listSettlementMaintain(this.queryParams).then(response => {
        this.settlementMaintainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        settlementNumber: undefined,
        clientId: undefined,
        clientName: undefined,
        clientPhone: undefined,
        clientType: undefined,
        clientCertificateType: undefined,
        clientCertificateNumber: undefined,
        manHourDiscountMoney: undefined,
        materialDiscountMoney: undefined,
        settlementTime: undefined,
        paymentMethod: undefined,
        paymentAccount: undefined,
        paymentAccountName: undefined,
        paymentVoucher: undefined,
        settlementRemark: undefined,
        collectionTime: undefined,
        collectionVoucher: undefined,
        collectionAccount: undefined,
        collectionSerialNumber: undefined,
        collectionRemark: undefined,
        state: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createDeptId: 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
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getSettlementMaintain(row.id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "结算维修单详情";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算维修单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSettlementMaintain(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) {
            updateSettlementMaintain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSettlementMaintain(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.settlementNumber + '"的数据项？').then(() => {
        this.loading = true;
        return delSettlementMaintain(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/settlementMaintain/export', {
        ...this.queryParams
      }, `报修结算单_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
