<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="nurseStationName">
        <el-button type class="stationbtn" @click="ParamsStation(true)">
          {{ queryParams.nurseStationName }}
        </el-button>
      </el-form-item>
      <el-form-item label="护理员姓名" prop="nursePersonName">
        <el-input
          v-model="queryParams.nursePersonName"
          placeholder="请输入护理员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="trainingOrderNo">
        <el-input
          v-model="queryParams.trainingOrderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="订单金额" prop="trainingOrderAmount">
        <el-input
          v-model="queryParams.trainingOrderAmount"
          placeholder="请输入订单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="订单状态" prop="trainingOrderStatus">
        <el-select
          v-model="queryParams.trainingOrderStatus"
          placeholder="请选择订单状态"
          clearable
        >
          <el-option
            v-for="item in trainingOrderStatusoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </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">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="trainingOrderList"
      @selection-change="handleSelectionChange"
    >
      ]
      <el-table-column
        label="所属护理站"
        align="center"
        prop="nurseStationName"
      />
      <el-table-column
        label="护理员姓名"
        align="center"
        prop="nursePersonName"
      />
      <el-table-column label="订单编号" align="center" prop="trainingOrderNo" />
      <el-table-column
        property="trainingItemTitle"
        label="培训课程名称"
        align="center"
      />
      <el-table-column
        label="订单状态"
        align="center"
        prop="trainingOrderStatus"
      >
        <template slot-scope="scope">
          <el-button v-if="scope.row.trainingOrderStatus == 'WAIT_PAY'"
            >待付款</el-button
          >
          <el-button
            type="primary"
            v-if="scope.row.trainingOrderStatus == 'PAY'"
            >已付款</el-button
          >
          <el-button
            type="warning"
            v-if="scope.row.trainingOrderStatus == 'WAIT_REFUND'"
            >退款中</el-button
          >

          <el-button
            type="danger"
            v-if="scope.row.trainingOrderStatus == 'CANCEL'"
            >已取消</el-button
          >
          <el-button
            type="success"
            v-if="scope.row.trainingOrderStatus == 'REFUNDED'"
            >已退款</el-button
          >
        </template>
      </el-table-column>

      <el-table-column
        label="订单金额"
        align="center"
        prop="trainingOrderAmount"
      />
      <el-table-column
        label="下单方式"
        align="center"
        prop="trainingOrderChannel"
      >
        <template slot-scope="scope">
          {{ scope.row.trainingOrderChannel == "MOBILE_APP" ? "手机App" : "" }}
          {{
            scope.row.trainingOrderChannel == "WECHAT_APPLET"
              ? "微信小程序"
              : ""
          }}
          {{
            scope.row.trainingOrderChannel == "ALI_PAY_APPLET"
              ? "支付宝小程序"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="下单时间"
        align="center"
        prop="trainingOrderTime"
        width="180"
      >
      </el-table-column>
      <!-- <el-table-column label="备注信息" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleUpdate(scope.row)"
            >查看</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="cencel(scope.row)"
            v-if="scope.row.trainingOrderStatus == 'PAY'"
            >退款</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="退款"
      :visible.sync="innerrefund"
      append-to-body
      width="500px"
    >
      <el-form :inline="true" :rules="rules" label-width="120px">
        <el-form-item label="订单编号" prop="trainingOrderNo">
          <el-input
            style="width: 260px"
            v-model="query.trainingOrderNo"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="培训课程名" prop="trainingItemTitle">
          <el-input
            style="width: 260px"
            v-model="query.trainingItemTitle"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="护理站名称" prop="nurseStationName">
          <el-input
            style="width: 260px"
            v-model="query.nurseStationName"
            clearable
            :disabled="true"
          />
        </el-form-item>

        <el-form-item label="退款金额" prop="trainingOrderAmount">
          <el-input
            v-model="query.trainingOrderAmount"
            style="width: 260px"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="护理员姓名" prop="nursePersonName">
          <el-input
            v-model="query.nursePersonName"
            style="width: 260px"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <!-- <el-form-item label="退款原因" prop="cancelAppointmentReason">
          <el-input
            style="width: 260px"
            v-model="query.cancelAppointmentReason"
            placeholder
            clearable
            type="textarea"
            :rows="5"
            disabled
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cencelbtn">取 消</el-button>
        <el-button type="primary" @click="ordercacenl">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看学习培训订单主对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="true"
      >
        <el-form-item label="护理员姓名" prop="nursePersonName">
          <div class="text">{{ form.nursePersonName }}</div>
        </el-form-item>
        <el-form-item label="订单编号" prop="trainingOrderNo">
          <div class="text">{{ form.trainingOrderNo }}</div>
        </el-form-item>
        <el-form-item label="订单金额" prop="trainingOrderAmount">
          <div class="text">{{ form.trainingOrderAmount }}</div>
        </el-form-item>

        <el-form-item label="下单方式" prop="trainingOrderChannel">
          <div class="text">{{ form.trainingOrderChannel }}</div>
        </el-form-item>
        <el-form-item label="下单时间" prop="trainingOrderTime">
          <div class="text">{{ form.trainingOrderTime }}</div>
        </el-form-item>
        <el-table
          :data="form.trainingOrderDetailsList"
          align="center"
          v-loading="loading"
          style="margin: 10px 0 20px 0; padding-bottom: 20px"
        >
          <el-table-column
            prop="trainingItemTitle"
            label="培训课程名称"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="trainingItemType"
            label="课程类型"
            align="center"
          >
            <template slot-scope="scope">
              {{
                scope.row.trainingItemType == "VIDEO_LEARNING" ? "视频学习" : ""
              }}
              {{
                scope.row.trainingItemType == "GRAPHIC_LEARNING"
                  ? "图文学习"
                  : ""
              }}
              {{
                scope.row.trainingItemType == "LIVE_COURSES" ? "直播课程" : ""
              }}
              {{
                scope.row.trainingItemType == "OFFLINE_TRAINING"
                  ? "线下培训"
                  : ""
              }}
              {{
                scope.row.trainingItemType == "LEARNING_ITEM_GROUP"
                  ? "学习培训课程组"
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-zoom-in"
                @click="handlelook(scope.row)"
                >查看</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-dialog>
    <!-- 查看图文 -->
    <el-dialog
      title="查看图文"
      :visible.sync="innerrefundvideo"
      append-to-body
      width="1000px"
    >
      <el-form
        :inline="true"
        :rules="rules"
        label-width="130px"
        :model="formlist"
      >
        <el-form-item
          label="培训课程详情"
          prop="trainingItemDetails"
          v-if="
            formlist.trainingItemType == 'VIDEO_LEARNING' ||
            formlist.trainingItemType == 'LEARNING_ITEM_GROUP'
          "
        >
          <div class="textdetails">{{ formlist.trainingItemDetails }}</div>
        </el-form-item>
        <el-form-item
          :label="
            formlist.trainingItemType == 'GRAPHIC_LEARNING'
              ? '培训课程内容'
              : formlist.trainingItemType == 'LEARNING_ITEM_GROUP'
              ? '培训课程介绍'
              : ''
          "
          prop="trainingItemContent"
          v-if="
            formlist.trainingItemType == 'GRAPHIC_LEARNING' ||
            formlist.trainingItemType == 'LEARNING_ITEM_GROUP'
          "
        >
          <editor
            @imgs="imgs"
            :url="'/common/uploadTrainingItemContentUrl'"
            v-model="formlist.trainingItemContent"
            :min-height="150"
            style="width: 600px"
          />
        </el-form-item>
        <el-form-item label="培训课程封面图片" prop="trainingItemCoverUrl">
          <img class="img" :src="baseurl + formlist.trainingItemCoverUrl" alt />
        </el-form-item>
        <el-form-item label="培训课程海报图片" prop="trainingItemPosterUrl">
          <img
            class="img"
            :src="baseurl + formlist.trainingItemPosterUrl"
            alt
          />
        </el-form-item>
        <el-table
          ref="list"
          v-if="formlist.trainingItemType == 'VIDEO_LEARNING'"
          v-loading="loading"
          style="margin-top: 20px; width: 1250px"
          :data="formlist.list"
        >
          <el-table-column label="章节视频" align="center" width="300">
            <template slot-scope="scope">
              <video
                class="img"
                :src="baseurl + scope.row.itemDirectoryUrl"
                controls="controls"
              ></video>
            </template>
          </el-table-column>
          <el-table-column label="章节名称" align="center">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.itemDirectoryName"
                placeholder="请输入章节名称"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="章节标题" align="center">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.itemDirectoryTitle"
                placeholder="请输入章节标题"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-dialog>
    <!-- 护理站 -->
    <el-dialog
      title="选择护理站"
      :visible.sync="nurseStationshow"
      width="1100px"
      append-to-body
      :before-close="clicknurseStationshow"
    >
      <el-form
        ref="form"
        :model="getListByUserquery"
        label-width="110px"
        :inline="true"
      >
        <el-form-item label="护理站编码" prop="nurseStationCode">
          <el-input
            v-model="getListByUserquery.nurseStationCode"
            placeholder="请输入护理站编码"
          />
        </el-form-item>
        <el-form-item label="护理站名称" prop="nurseStationName">
          <el-input
            v-model="getListByUserquery.nurseStationName"
            placeholder="请输入护理站名称"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="info"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="stationcancel"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="nurseStationlist"
        @cell-dblclick="choicestationid"
      >
        <el-table-column label="请选择" width="70" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="width: 15px; height: 15px"
              circle
              @click="choicestationid(scope.row)"
              v-if="queryParams.nurseStationId == scope.row.id"
            ></el-button>

            <el-button
              style="width: 15px; height: 15px"
              circle
              v-else
              @click="choicestationid(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column
          property="nurseStationCode"
          label="护理站编码"
          align="center"
        ></el-table-column>
        <el-table-column
          property="nurseStationName"
          label="护理站名称"
          align="center"
        ></el-table-column>
        <el-table-column
          property="phone"
          label="联系电话"
          align="center"
        ></el-table-column>
        <el-table-column
          property="address"
          label="护理站地址"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
      </el-table>

      <pagination
        v-show="stationtotal > 0"
        :total="stationtotal"
        :page.sync="getListByUserquery.pageNum"
        :limit.sync="getListByUserquery.pageSize"
        @pagination="info"
      />
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listTrainingOrder,
  getTrainingOrder,
  orderItemDirectory,
  refundInformation,
  delTrainingOrder,
  xylWeChatRefundNotify,
  addTrainingOrder,
  updateTrainingOrder,
} from "@/api/system/trainingOrder";
import baseurl from "@/api/baseurl.js";
import stationAcatar from "../stationAvatar/index.vue";
import editor from "@/components/Editor";
import { getListByUser } from "@/api/system/userlist.js";
export default {
  name: "TrainingOrder",
  components: { stationAcatar, editor },
  data() {
    return {
      baseurl: null,
      trainingOrderStatusoptions: [
        {
          value: "WAIT_PAY",
          label: "待付款",
        },
        {
          value: "PAY",
          label: "已付款",
        },
        {
          value: "WAIT_REFUND",
          label: "退款中",
        },
        {
          value: "REFUNDED",
          label: "已退款",
        },
        {
          value: "CANCEL",
          label: "已取消",
        },
      ],
      nurseStationshow: false,

      // 遮罩层
      loading: true,
      innerrefund: false,
      innerrefundvideo: false,
      query: {},
      formlist: {},
      trainingItemTypeoptions: [
        {
          value: "GRAPHIC_LEARNING",
          label: "图文学习",
        },
        {
          value: "LEARNING_ITEM_GROUP",
          label: "学习培训项目组",
        },
        {
          value: "VIDEO_LEARNING",
          label: "视频学习",
        },
      ],
      // 选中数组
      ids: [],
      optiondeflag: [
        {
          value: 0,
          label: "否",
        },
        {
          value: 1,
          label: "是",
        },
      ],
      options: [
        {
          value: "MOBILE_APP",
          label: "手机App",
        },
        {
          value: "WECHAT_APPLET",
          label: "微信小程序",
        },
        {
          value: "ALI_PAY_APPLET",
          label: "支付宝小程序",
        },
      ],
      getListByUserquery: {
        pageNum: 1,
        pageSize: 10,
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      stationtotal: 0,
      // 学习培训订单主表格数据
      trainingOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nurseStationId: null,
        trainingOrderAmount: null,
        nurseStationPersonId: null,
        nursePersonName: null,
        trainingOrderNo: null,
        trainingOrderStatus: null,
        trainingOrderAmount: null,
        trainingOrderChannel: null,
        trainingOrderTime: null,
      },
      // 表单参数
      form: {},
      list: [],
      nurseStationlist: [],
      trainingOrderDetailsList: [],
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.baseurl = baseurl;
  },
  methods: {
    //页面所属护理站
    ParamsStation(item) {
      this.info();
      this.nurseStationshow = true;
    },
    //护理站list
    info() {
      getListByUser(this.getListByUserquery).then((res) => {
        this.nurseStationlist = res.rows;
        this.stationtotal = res.total;
      });
    },
    clicknurseStationshow() {
      this.nurseStationshow = false;
    },
    //护理站重置
    stationcancel() {
      this.getListByUserquery = {
        pageNum: 1,
        pageSize: 10,
      };
      this.info();
    },
    //护理站页面选择护理站
    choicestationid(item) {
      this.queryParams.nurseStationName = item.nurseStationName;
      this.queryParams.nurseStationId = item.id;
      this.nurseStationshow = false;
    },
    // 章节视频
    itemDirectoryUrl(item) {
      let items = JSON.parse(item);
      console.log(items);
      if (items.idd && !items.trainingItemDirectoryId) {
        this.form.trainingItemDirectoryList.forEach((e) => {
          if (e.idd == items.idd) {
            e.itemDirectoryUrl = items.itemDirectoryUrl;
          }
        });
      } else if (!items.idd && items.attributeDetailsId) {
        this.form.trainingItemDirectoryList.forEach((e) => {
          if (e.attributeDetailsId == items.attributeDetailsId) {
            e.itemDirectoryUrl = items.itemDirectoryUrl;
          }
        });
      }
    },
    imgUrl4(imgUrl) {},
    // 取消按钮
    cencelbtn() {
      this.innerrefund = false;
    },
    // // 确认退款
    cencel(row) {
      console.log(row);
      refundInformation(row.id).then((response) => {
        this.query = response.data;
        this.innerrefund = true;
      });
    },
    imgs(item) {
      console.log(item);
      this.imgsurl.pictureUrlList.push(item);
    },
    //退款确定按钮
    ordercacenl() {
      var obj = {
        orderNo: this.query.trainingOrderNo,
        refundPrice: this.query.trainingOrderAmount,
        trainingOrderFlag: "TRAINING",
      };
      xylWeChatRefundNotify(obj).then((res) => {
        if (res.code == 200) {
          this.$modal.msgSuccess("退款有延迟，请耐心等待");
        }
        this.getList();
        this.innerrefund = false;
      });
    },
    /** 查询学习培训订单主列表 */
    getList() {
      this.loading = true;
      listTrainingOrder(this.queryParams).then((response) => {
        this.trainingOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 查看视频
    handlelook(row) {
      this.loading = true;
      this.innerrefundvideo = true;
      console.log(row);
      this.formlist.trainingItemCoverUrl = row.trainingItemCoverUrl;
      this.formlist.trainingItemPosterUrl = row.trainingItemPosterUrl;
      this.formlist.trainingItemDetails = row.trainingItemDetails;
      this.formlist.trainingItemContent = row.trainingItemContent;
      this.formlist.trainingItemType = row.trainingItemType;
      orderItemDirectory(row.trainingOrderDetailsId).then((response) => {
        this.formlist.list = response.rows;
        this.loading = false;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        nurseStationId: null,
        nurseStationPersonId: null,
        nursePersonName: null,
        trainingOrderNo: null,
        trainingOrderStatus: null,
        trainingOrderAmount: null,
        trainingOrderChannel: null,
        trainingOrderTime: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      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 = "添加学习培训订单主";
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTrainingOrder(id).then((response) => {
        console.log(response);
        this.form = response.data;
        this.form.trainingOrderDetailsList =
          response.data.trainingOrderDetailsList;
        response.data.trainingOrderChannel == "WECHAT_APPLET"
          ? (response.data.trainingOrderChannel = "微信小程序")
          : "";
        response.data.trainingOrderChannel == "MOBILE_APP"
          ? (response.data.trainingOrderChannel = "手机App")
          : "";
        response.data.trainingOrderChannel == "ALI_PAY_APPLET"
          ? (response.data.trainingOrderChannel = "支付宝小程序")
          : "";

        this.open = true;
        this.title = "查看学习培训订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTrainingOrder(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTrainingOrder(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除学习培训订单主编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTrainingOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/trainingOrder/export",
        {
          ...this.queryParams,
        },
        `trainingOrder_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  <style lang="scss">
.img {
  width: 150px;
  height: 150px;
  margin-left: 10px;
}
.text {
  width: 250px;
  height: 36px;
  line-height: 36px;
  padding-left: 15px;
  font-size: 14px;
  border-radius: 4px;
  padding: 0 15px;
  border: 1px solid #e6ebf5;
}
.textdetail {
  width: 550px;
  // height: 600px;
  line-height: 36px;
  padding-left: 15px;
  font-size: 14px;
  border-radius: 4px;
  padding: 0 15px;
  border: 1px solid #e6ebf5;
}
.stationbtn {
  width: 208px;
  text-align: left;
  height: 32px;
  overflow: hidden;
  font-size: 14px;
}
.textdetails {
  width: 600px;
  // height: 300px;
  line-height: 36px;
  padding-left: 15px;
  font-size: 14px;
  border-radius: 4px;
  padding: 0 15px;
  border: 1px solid #e6ebf5;
}
::v-deep .wihi {
  text-align: left !important;
  // width: 208px !important;
  // height: 208px !important;
  background: none;
}
::v-deep .video-avatar {
  transform: translateX(-25%);
}
</style>