<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="tradeId">
        <el-input
          v-model="queryParams.tradeId"
          clearable
          placeholder="请输入订单编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="subject">
        <el-select v-model="queryParams.body" clearable placeholder="订单类型">
          <el-option
            v-for="dict in bodyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="付款状态" prop="fundType">
        <el-select
          v-model="queryParams.fundType"
          clearable
          placeholder="付款状态"
        >
          <el-option
            v-for="dict in fundTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="timestamp">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          size="small"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @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
          v-hasPermi="['system:lkTrade:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:lkTrade:remove']"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          @click="handleClear"
          >清空</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:lkTrade:export']"
          icon="el-icon-download"
          size="mini"
          type="warning"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:lkTrade:refund']"
          :disabled="multiple"
          icon="el-icon-s-fold"
          size="mini"
          type="danger"
          @click="handlereFund"
          >退款</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="lkTradeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="订单编号" prop="tradeId" />
      <el-table-column align="center" label="订单类型" prop="subject" />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="商户交易号"
        prop="outTradeNo"
      />
      <el-table-column align="center" label="交易金额" prop="totalAmount" />
      <el-table-column
        :formatter="tradeStatusFormat"
        align="center"
        label="订单状态"
        prop="fundType"
      />
      <el-table-column
        :formatter="carTypeFormat"
        align="center"
        label="卡类型"
        prop="cardType"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="卡号"
        prop="cardNo"
      />
      <el-table-column align="center" label="工号" prop="refundType" />
      <el-table-column
        align="center"
        label="下单时间"
        prop="timestamp"
        width="180"
      />
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            icon="el-icon-view"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改商品交易订单对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="800px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        class="scroll"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单编号:" prop="tradeId">
              {{ form.tradeId }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单描述:" prop="body">
              {{ form.body }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单类型:" prop="subject">
              {{ form.subject }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易号:" prop="outTradeNo">
              {{ form.outTradeNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易金额:" prop="totalAmount">
              {{ form.totalAmount }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单状态:" prop="tradeStatus">
              {{ form.tradeStatus }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下单时间:" prop="timestamp">
              {{ form.timestamp }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付时间:" prop="gmtPayment">
              {{ form.gmtPayment }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额:" prop="tradeAmount">
              {{ form.tradeAmount }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款号:" prop="outRequestNo">
              {{ form.outRequestNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额:" prop="refundAmount">
              {{ form.refundAmount }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款时间:" prop="gmtRefundPay">
              {{ form.gmtRefundPay }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处方号:" prop="presNos">
              {{ form.presNos }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约号:" prop="reservationNo">
              {{ form.reservationNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡类型:" prop="cardType">
              {{ viewCarTypeFormat(form.cardType) }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡号:" prop="cardNo">
              {{ form.cardNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住院号:" prop="hosCardNo">
              {{ form.hosCardNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="院内编号:" prop="patientId">
              {{ form.patientId }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号:" prop="refundType">
              {{ form.refundType }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易状态:" prop="fundType">
              {{ fundTypeStatusFormat(form.fundType) }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改时间:" prop="updateTime">
              {{ form.updateTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信Id:" prop="openId">
              {{ form.openId }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="HIS流水号:" prop="hisNo">
              {{ form.hisNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="HIS确认结果:" prop="hisConfirmResult">
              {{ form.hisConfirmResult }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLkTrade,
  getLkTrade,
  delLkTrade,
  addLkTrade,
  updateLkTrade,
  exportLkTrade,
  clearTable,
  fundLkTrade
} from "@/api/system/lkTrade";

export default {
  name: 'LkTrade',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商品交易订单表格数据
      lkTradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [],
      // 状态数据字典
      typeOptions: [],
      cardTypeOptions: [],
      bodyOptions: [],
      dateRange: [],
      payOptions: [],
      fundTypeOptions: [
        { dictLabel: "已付款", dictValue: "1" },
        { dictLabel: "未付款", dictValue: "0" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createBy: undefined,
        body: undefined,
        subject: undefined,
        outTradeNo: undefined,
        totalAmount: undefined,
        tradeStatus: undefined,
        timestamp: undefined,
        gmtPayment: undefined,
        tradeAmount: undefined,
        outRequestNo: undefined,
        refundAmount: undefined,
        gmtRefundPay: undefined,
        presNos: undefined,
        reservationNo: undefined,
        cardType: undefined,
        cardNo: undefined,
        hosCardNo: undefined,
        patientId: undefined,
        refundType: undefined,
        fundType: undefined,
        updateTime: undefined,
        openId: undefined,
        hisNo: undefined,
        hisConfirmResult: undefined,
        beginTime: undefined,
        endTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xxx: [{ required: true, message: "xxx不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("pay_card_type").then(response => {
      this.cardTypeOptions = response.data;
    });
    this.getDicts("pay_title").then(response => {
      this.bodyOptions = response.data;
    });
    this.getDicts("app_pay_status").then(response => {
      this.payOptions = response.data;
    });
  },
  methods: {
    handleClear() {
      clearTable();
    },
    rangeDateToAre() {
      if (this.dateRange != null) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
    },
    carTypeFormat(row) {
      return this.selectDictLabel(this.cardTypeOptions, row.cardType);
    },
    tradeStatusFormat(row) {
      return this.selectDictLabel(this.payOptions, row.fundType);
    },
    fundTypeStatusFormat(val) {
      return this.selectDictLabel(this.payOptions, val);
    },
    viewCarTypeFormat(val) {
      return this.selectDictLabel(this.cardTypeOptions, val);
    },
    /** 查询商品交易订单列表 */
    getList() {
      this.loading = true;
      listLkTrade(this.queryParams).then(response => {
        this.lkTradeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        body: undefined,
        subject: undefined,
        outTradeNo: undefined,
        totalAmount: undefined,
        tradeStatus: undefined,
        timestamp: undefined,
        gmtPayment: undefined,
        tradeAmount: undefined,
        outRequestNo: undefined,
        refundAmount: undefined,
        gmtRefundPay: undefined,
        presNos: undefined,
        reservationNo: undefined,
        cardType: undefined,
        cardNo: undefined,
        hosCardNo: undefined,
        patientId: undefined,
        refundType: undefined,
        fundType: undefined,
        updateTime: undefined,
        openId: undefined,
        hisNo: undefined,
        hisConfirmResult: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.rangeDateToAre();
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tradeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品交易订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.tradeId || this.ids;
      getLkTrade(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "商品交易订单详情";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tradeId != undefined) {
            updateLkTrade(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addLkTrade(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 退款按钮 */
    handlereFund(row) {
      console.log(row)
      const ids = row.tradeId || this.ids
      this.$confirm('是否确认为订单号为"' + ids + '"退款?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return fundLkTrade(ids)
        })
        .then(res => {
          this.getList()
          if (res.code === "200") {
            this.msgSuccess(res.msg);
          } else if (res.code === "201") {
            this.msgSuccess(res.msg);
          } else {
            this.msgSuccess(res.msg);
          }
        })
        .catch(function() {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.tradeId || this.ids;
      this.$confirm('是否确认删除tradeId为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delLkTrade(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有商品交易订单数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          debugger;
          return exportLkTrade(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
<style scoped>
.scroll {
  height: 550px;
  overflow: auto;
}
</style>
