<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item label="支付状态" prop="isPay">
        <!-- <el-input
          v-model="queryParams.isPay"
          placeholder="请输入支付状态 0 待支付 1已支付"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.isPay" placeholder="请选择" clearable size="small">
          <el-option label="待支付" value="0" />
          <el-option label="已支付" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间" prop="payDateRange">
        <el-date-picker
          v-model="payDateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option label="待支付" value="1" />
          <el-option label="已支付" value="2" />
          <el-option label="已完成" value="3" />
          <el-option label="申请退款" value="-1" />
          <el-option label="已退款" value="-2" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款时间" prop="refundDateRange">
       <el-date-picker
          v-model="refundDateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 240px"
        />
      </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="['fsCourseProductOrder:fsCourseProductOrder: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="['fsCourseProductOrder:fsCourseProductOrder: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="['fsCourseProductOrder:fsCourseProductOrder:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['course:fsCourseProductOrder:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleDecodeExport"
          v-hasPermi="['course:fsCourseProductOrder:decodeExport']"
        >解密手机号导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="fsCourseProductOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" width="180px" align="center" prop="orderCode" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="用户电话" width="180px" align="center" prop="userPhone" >
        <template slot-scope="scope">
          <span>{{ scope.row.userPhone }}<el-button icon="el-icon-search" size="mini" @click="handlePhone(scope)" style="margin-left: 20px;" circle v-hasPermi="['course:fsCourseProductOrder:queryPhone']"></el-button></span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="productName"/>
      <el-table-column label="课程" align="center" :show-overflow-tooltip="true" prop="courseName" />
      <el-table-column label="视频" align="center" :show-overflow-tooltip="true" prop="title" />
      <el-table-column label="公司名称" align="center"  prop="companyName" />
      <el-table-column label="销售" align="center"  prop="companyUserName" />
      <el-table-column label="企微外部联系人" align="center"  prop="extName" />
      <el-table-column label="支付状态" align="center" prop="isPay">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPay == 0">待支付</el-tag>
          <el-tag v-if="scope.row.isPay == 1" type="success">已支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1">待支付</span>
          <span v-if="scope.row.status === 2">已支付</span>
          <span v-if="scope.row.status === 3">已完成</span>
          <span v-if="scope.row.status === -1">申请退款</span>
          <span v-if="scope.row.status === -2">退款成功</span>
        </template>
      </el-table-column>
      <el-table-column label="申请退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请退款理由" :show-overflow-tooltip="true" align="center" prop="refundExplain" />
      <el-table-column label="核销码" align="center" prop="verifyCode" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status == 1 || scope.row.status == 3 || scope.row.status == -2"
            size="mini"
            type="text"
            @click="handleRefund(scope.row)"
            v-hasPermi="['course:fsCourseProductOrder:refund']"
          >退款</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="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="用户电话" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入用户电话" />
        </el-form-item>
        <el-form-item label="拍单商品id" prop="courseProductId">
          <el-input v-model="form.courseProductId" placeholder="请输入拍单商品id" />
        </el-form-item>
        <el-form-item label="商品json" prop="productJson">
          <el-input v-model="form.productJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="课程id" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入课程id" />
        </el-form-item>
        <el-form-item label="视频小节id" prop="videoId">
          <el-input v-model="form.videoId" placeholder="请输入视频小节id" />
        </el-form-item>
        <el-form-item label="支付状态 0 待支付 1已支付" prop="isPay">
          <el-input v-model="form.isPay" placeholder="请输入支付状态 0 待支付 1已支付" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable size="small"
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式 微信" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付方式 微信">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="1 待支付 2已支付 3 已完成 -1 申请退款 -2 : 退款成功">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="申请退款时间" prop="refundTime">
          <el-date-picker clearable size="small"
            v-model="form.refundTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择申请退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请退款理由" prop="refundExplain">
          <el-input v-model="form.refundExplain" placeholder="请输入申请退款理由" />
        </el-form-item>
        <el-form-item label="核销码" prop="verifyCode">
          <el-input v-model="form.verifyCode" placeholder="请输入核销码" />
        </el-form-item>
        <el-form-item label="公司id" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司id" />
        </el-form-item>
        <el-form-item label="销售id" prop="companyUserId">
          <el-input v-model="form.companyUserId" placeholder="请输入销售id" />
        </el-form-item>
        <el-form-item label="企微外部联系人id" prop="extId">
          <el-input v-model="form.extId" placeholder="请输入企微外部联系人id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFsCourseProductOrder,
  getFsCourseProductOrder,
  delFsCourseProductOrder,
  addFsCourseProductOrder,
  updateFsCourseProductOrder,
  exportFsCourseProductOrder,
  refund,
  getOrderUserPhone,
  exportFsCourseProductOrderDecodePhone
} from "@/api/course/fsCourseProductOrder";

export default {
  name: "FsCourseProductOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拍单商品订单表格数据
      fsCourseProductOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      payDateRange: [],
      refundDateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        userId: null,
        userName: null,
        userPhone: null,
        courseProductId: null,
        productJson: null,
        courseId: null,
        videoId: null,
        isPay: null,
        payStartTime: null,
        payEndTime: null,
        payType: null,
        status: null,
        refundStartTime: null,
        refundEndTime: null,
        refundExplain: null,
        verifyCode: null,
        companyId: null,
        companyUserId: null,
        extId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询拍单商品订单列表 */
    getList() {
      this.loading = true;
      if (this.payDateRange && this.payDateRange.length === 2) {
        this.queryParams.payStartTime = this.payDateRange[0];
        this.queryParams.payEndTime = this.payDateRange[1].substring(0,10)+" 23:59:59";
      } else {
        this.queryParams.payStartTime = null;
        this.queryParams.payEndTime = null;
      }
      if (this.refundDateRange && this.refundDateRange.length === 2) {
        this.queryParams.refundStartTime = this.refundDateRange[0];
        this.queryParams.refundEndTime = this.refundDateRange[1].substring(0,10)+" 23:59:59";
      } else {
        this.queryParams.refundStartTime = null;
        this.queryParams.refundEndTime = null;
      }
      listFsCourseProductOrder(this.queryParams).then(response => {
        this.fsCourseProductOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseOrderId: null,
        orderCode: null,
        userId: null,
        userName: null,
        userPhone: null,
        courseProductId: null,
        productJson: null,
        courseId: null,
        videoId: null,
        isPay: null,
        payTime: null,
        payType: null,
        status: 0,
        createTime: null,
        refundTime: null,
        refundExplain: null,
        verifyCode: null,
        updateTime: null,
        companyId: null,
        companyUserId: null,
        extId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.payDateRange = [];
      this.refundDateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseOrderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拍单商品订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseOrderId = row.courseOrderId || this.ids
      getFsCourseProductOrder(courseOrderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拍单商品订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseOrderId != null) {
            updateFsCourseProductOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFsCourseProductOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseOrderIds = row.courseOrderId || this.ids;
      this.$confirm('是否确认删除拍单商品订单编号为"' + courseOrderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFsCourseProductOrder(courseOrderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleDecodeExport() {
      if (this.payDateRange && this.payDateRange.length === 2) {
        this.queryParams.payStartTime = this.payDateRange[0];
        this.queryParams.payEndTime = this.payDateRange[1].substring(0,10)+" 23:59:59";
      } else {
        this.queryParams.payStartTime = null;
        this.queryParams.payEndTime = null;
      }
      if (this.refundDateRange && this.refundDateRange.length === 2) {
        this.queryParams.refundStartTime = this.refundDateRange[0];
        this.queryParams.refundEndTime = this.refundDateRange[1].substring(0,10)+" 23:59:59";
      } else {
        this.queryParams.refundStartTime = null;
        this.queryParams.refundEndTime = null;
      }
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有拍单商品订单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportFsCourseProductOrderDecodePhone(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.payDateRange && this.payDateRange.length === 2) {
        this.queryParams.payStartTime = this.payDateRange[0];
        this.queryParams.payEndTime = this.payDateRange[1].substring(0,10)+" 23:59:59";
      } else {
        this.queryParams.payStartTime = null;
        this.queryParams.payEndTime = null;
      }
      if (this.refundDateRange && this.refundDateRange.length === 2) {
        this.queryParams.refundStartTime = this.refundDateRange[0];
        this.queryParams.refundEndTime = this.refundDateRange[1].substring(0,10)+" 23:59:59";
      } else {
        this.queryParams.refundStartTime = null;
        this.queryParams.refundEndTime = null;
      }
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有拍单商品订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFsCourseProductOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },

    /**申请退款 */
    handleRefund(row) {
      const data = {
        courseOrderId : row.courseOrderId
      };
       this.$confirm('是否确认拍商品退款?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return refund(data);
        }).then(response => {
          this.getList();
          this.msgSuccess("退款成功");
        }).catch(() => {});
    },

    /**查询电话 */
    handlePhone(scope) {
      getOrderUserPhone(scope.row.courseOrderId).then(res=>{
        scope.row.userPhone = res.userPhone
      })
    },
  }
};
</script>
