<template>
  <div class="m-right-block fr mh576">
    <div class="right-block-bd ng-scope" ui-view="myStudyContent" style="position: relative;">
      <div class="panel-tit clear">
        <p class="fl">推荐课程</p>
      </div>

      <ul class="pt-line mt20">
        <li :span="6" v-for="item in typeList" style="width: 25%;float: left;" :key="item.id" v-if="item.isOpen">
          <span :class="{active:item.name==type }" class="nav-btn w220" @click="type=item.name">{{item.name}} </span>
        </li>
      </ul>

      <div class="mt10" v-if="type=='施工现场专业人员'">
        <p style="color: red;font-size: 16px;">
          报名对象为：2020年度及以后由培训单位核发的和2021年度换发的“电子培训合格证”且每年度需参加继续教育人员
        </p>
        <p style="color: red;font-size: 16px;">
          注意：继续教育学员请先下载
          <a style="color: blue;cursor: hand;" href="https://sm.hqedust.com/cdn/%E7%BB%A7%E7%BB%AD%E6%95%99%E8%82%B2%E5%A7%94%E6%89%98%E5%9F%B9%E8%AE%AD%E3%80%81%E6%89%BF%E8%AF%BA%E5%87%BD.doc">
            《继续教育委托培训、承诺函》
          </a>
            ，填写并加盖单位公章后，在申请开通课程界面上传扫描件、并缴费
        </p>
      </div>

      <ul class="m-course-list clear mt10 classlist" v-if="total>0">
        <li v-for="(item,index) in list" :key="item.course_id" class="pt-sola mt20 pb10">
          <div>
            <span v-if="item.isFinish==1" class="p-ico2 ico-passed"></span>
            <div class="img">
              <img :src="item.tb">
              <div class="year-label ng-binding">
                <span v-if="item.is_pay==0 && item.buyImg =='' && item.payNo == ''">
                  还未购买
                </span>
                <span v-else-if="item.is_pay==0" style="background-color: orange;">
                  等待审核
                </span>
                <span v-else-if="item.is_pay==2" style="background-color: #78335F;">申请驳回</span>
                <span v-else-if="!item.is_pass" style="background-color: blue;padding: 2px;border-radius: 2px;">在学习中</span>
                <span v-else style="background-color: green;padding: 2px;border-radius: 2px;">已经完成</span>
              </div>

              <div class="hover-block">
                <div class="mask-bg"></div>
                <el-button @click="gotoPlay(item.course_id)" v-if="item.is_pay==1" class="ui-btn">进入学习</el-button>

                <el-button @click="goPay(index)" v-else-if="item.is_pay==2 || !!item.buyImg" class="ui-btn btn-o">补充材料</el-button>
                <el-button @click="goPay(index)" v-else class="ui-btn btn-o">购买课程</el-button>
              </div>
            </div>

            <div class="tit"><a>{{item.name}}</a></div>
            <div class="info">
              <p class="w1"><span class="p-ico c1"></span> {{item.type}}</p>
              <p class="w2"><span class="p-ico c2"></span>{{item.nd||'2020'}}</p>
              <p class="w1">
                <span class="p-ico c5"></span> ￥{{item.fee/100}}元
              </p>
              <p class="w2">
                <span class="p-ico c3"></span> {{item.xs/10}}学时
              </p>
            </div>
          </div>
        </li>
      </ul>

      <el-pagination class="m-pages" @current-change="handleCurrentChange" :current-page="page" :page-size="size"
        layout="total, prev, pager, next" :total="total">
      </el-pagination>

    </div>

    <el-dialog class="fc tc" :visible.sync="loginDialog" :close-on-click-modal="false" width="500px" @close="loginDialog=false">
      <div class="login-dialog m-login">
        <un-login @login="login"></un-login>
      </div>
    </el-dialog>


    <el-dialog title="申请开通课程"  :visible.sync="buyCourseDialog" :append-to-body="true"
    width="800px" top="20px" align="center">
      <el-form label-width="80px" label-position="right" :inline="true" :model="buyForm" status-icon :rules="rules" ref="elForm">
        <el-row align="left">
          <el-col :lg="12" class="pl20 pr20">
            <el-form-item lable="报考岗位" prop="courseName">
              <label slot="label">报考岗位</label>
              <el-input v-model="buyForm.courseName" type="text" placeholder="报考岗位" style="width: 220px;"></el-input>
            </el-form-item>

          </el-col>

          <el-col :lg="12" class="pl20 pr20">
            <el-form-item lable="证书编号" prop="rzCode">
              <label slot="label">证书编号</label>
              <el-input v-model="buyForm.rzCode" type="text" placeholder="证书编号" style="width: 220px;"></el-input>
            </el-form-item>
          </el-col>

          <el-row>
            <span style="color: red;padding:10px"> 请认真核实报考岗位与证书编号，本课程不支持退款</span>
          </el-row>

          <el-col :lg="12" class="pl20 pr20">
            <el-form-item lable="邮箱地址" prop="email">
              <label slot="label">邮箱地址</label>
              <el-input v-model="buyForm.email" type="textarea" placeholder="邮箱地址" style="width: 220px;"></el-input>
            </el-form-item>
          </el-col>


          <el-col :span="12" class="pl20 pr20">
            <el-form-item lable="订单号" prop="payNo">
              <label slot="label">订单编号</label>
              <el-input v-model="buyForm.payNo" placeholder="输入订单编号后6位" style="width: 220px;" :minlength="6" :maxlength="6" show-word-limit ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
           <el-col :span="12" class="pl20 pr20">
             <el-form-item lable="工作单位" prop="company">
               <label slot="label">工作单位</label>
               <el-input v-model="buyForm.company" type="textarea" placeholder="工作单位" style="width: 220px;"></el-input>
             </el-form-item>
           </el-col>
          <el-col :span="12" class="pl20 pr20">
            <el-form-item lable="备注信息" prop="marks">
              <label slot="label">备注信息</label>
              <el-input v-model="buyForm.marks" type="textarea" placeholder="姓名+工种名称，例：张三+土建施工员" style="width: 220px;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="type=='施工现场专业人员'">
           <img :src="buyForm.buyImg" :onerror="errorImg" style="width: 160px;text-align: center;">
           <do-upload @onFinish="(url)=>{buyForm.buyImg=url}" placeholder="上传承诺书" width="160px"> </do-upload>
        </el-row>
        <el-row>
          <h2 class="tc"> 本课程费用: <strong style="color: red;"> {{buyForm.fee/100}} 元 </strong> </h2>
          <div style="width: 300px; margin: 0px auto;">
            <img src="/static/images/wxpay.jpg"   width="300px" @click="showImg(buyForm.buyImg)">
          </div>
        </el-row>

         <el-row class="tc fc">
          <el-form-item >
            <el-button @click="buyCourseDialog=false">取 消</el-button>
            <el-button @click="buyCourse" type="primary">确 定</el-button>
          </el-form-item>
        </el-row>

      </el-form>

    </el-dialog>



    <el-dialog title="承诺书" class="fc tc" :visible.sync="commitmentDialog">

        <div>
          <div v-if="type=='施工现场专业人员'">
            <div class="commitment-header"> 致：三明市城市建设项目服务中心 </div>
            <div class="tl commitment">
                {{promise.promise_edu}}
            </div>
            <div class="commitment-footer">
              特此诺书！
            </div>
          </div>

          <div v-else-if="type=='建筑施工企业特种作业人员'">
            <div class="tl commitment">
              {{promise.promise_special}}
            </div>

          </div>


          <div v-else-if="type=='园林绿化技术工人'">
            <div class="tl commitment">
              {{promise.promise_plant}}
            </div>
          </div>

          <div v-else>
            <div class="tl commitment">
              {{promise.promise_other}}
            </div>
          </div>


          <div class="tl commitment-content mt20">
            <el-radio-group v-model="isCommitment">
              <el-row class="mt10">
                <el-radio :label="true">本人作出上述承诺</el-radio>
              </el-row>
              <el-row class="mt10">
                <el-radio :label="false">本人不作出上述承诺</el-radio>
              </el-row>
            </el-radio-group>
          </div>

          <span slot="footer" class="dialog-footer">
            <el-button @click="commitmentDialog = false">取 消</el-button>
            <el-button type="primary" :disabled="isCommitment?false:true" @click="buyCourseDialog=true">确 定</el-button>
          </span>
        </div>

    </el-dialog>

    <el-dialog append-to-body close-on-click-modal :visible.sync="showImgDialog" style="margin-top: 0px;" :width="width">
      <img :src="imgUrl" @load="onLoad" alt="" />
    </el-dialog>
  </div>
</template>

<script>
  import {
    httpServer
  } from "@/components/httpServer/httpServer.js";

  import promise from "./promise.js";
  import {
    MessageBox
  } from "element-ui";
  import unLogin from "../../login/components/unLogin.vue";
  import {
    parseTime
  } from "@/utils";
  import DoUpload from '@/components/upload/index.vue'
  import exampleImg from '@/assets/template.png'


  import {
    mapGetters,
    mapActions
  } from "vuex";

  export default {
    name: "Index",
    data() {
      return {
        page: 1,
        size: 9,
        total: 0,
        stage: 0,
        isCompanyPay: 0,
        width: "",
        type: '',
        list: [],
        showData: [],
        isCommitment: "",
        buyCourseDialog: false,
        showExample: false,
        showImgDialog: false,
        buyForm: {
          marks: '',
          rzCode: '',
          payNo:'',
          email:'',
          company:'',
          buyImg:'',
          courseName: ''
        },
        promise,
        imgUrl: '',
        qrcodeUrl: "",
        outTradeNo: "",
        courseFee: 0,
        title: '',
        timer: null,
        tickCount: 0,
        listLoading: false,
        loginDialog: false,
        isLoginOk: false,
        media: {},
        commitmentDialog: false,
        exampleImg: exampleImg,
        rules: {
          rzCode: [{
            required: true,
            message: '请输入证书编号'
          }],
          payNo:[
            {
              required: true,
              message: '请输入订单编号后六位'
            }
          ],
          marks:[
            {
              required: true,
              message: '请输入备注信息'
            }
          ],
          email: [{
              required: true,
              message: '请输入有效邮箱'
            },
            {
              type: 'string',
              message: '邮箱格式不正确',
              trigger: 'blur',
              transform(value) {
                if (!/^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value)) {
                  return true
                } else {}
              }
            }
          ],
          company: [{
            required: true,
            message: '请输入所在企业'
          }],
          courseName: [{
            required: true,
            message: '请输入报考岗位'
          }]
        },
        errorImg: 'this.src="' + require('../../../assets/images/no-data_hash5abcd2ef62.png') + '"'
      };
    },
    computed: {
      ...mapGetters("user", ["typeList", "userInfo"])
    },
    components: {
      unLogin,
      DoUpload
    },
    filters: {
      dateFilter(val) {
        return parseTime(val, '{y}-{m}-{d}')
      }
    },
    watch: {
      type(val) {
        this.page = 1
        this.getData()
      },
      isCommitment(val) {
        console.log(val)
      }
    },
    beforeMount() {
      this.type = this.$route.query.type||'施工现场专业人员'
      if (!this.type && this.typeList.length > 0) {
        this.type = this.typeList[0].name
        this.getData()
      }
    },
    beforeDestroy() {
      if (this.timer) window.clearInterval(this.timer);
    },
    methods: {
      showImg(url) {
        if (!url) return;
        this.imgUrl = url;
        this.showImgDialog = true;
      },
      closeDialog() {
        if (this.timer) window.clearInterval(this.timer);
      },
      onLoad(e) {
        const img = e.target;
        let width = 0;
        if (img.fileSize > 0 || (img.width > 1 && img.height > 1)) {
          width = img.width + 40;
        }
        this.width = width + "px";
      },
      getData() {
        if (!this.type) return;
        let param = {
          size: this.size,
          type: this.type,
          from: (this.page - 1) * this.size
        }
        httpServer("course.getcoursemarket", param).then((res) => {
          if (res.code == 200) {
            this.list = res.data.list;
            this.total = res.data.total;
          }
        });
      },
      buyCourse() {
        this.$refs["elForm"].validate((valid) => {
          if (!valid) return;
          let { marks, company, payNo, course_id, courseName, rzCode, name,  email} = this.buyForm;
          if (courseName != name) {
            this.$message.errorMsg("报考岗位有误", 1)
            return;
          }
          let param = { marks,payNo, company,courseId: course_id, courseName,rzCode,email }
          httpServer("course.buyCourse", param).then((res) => {
            if (res.code == 200) {
              this.$message.successMsg("申请购买成功", 1);
              this.buyForm.temp.marks = this.buyForm.marks
              this.buyForm.temp.is_pay = 0
              this.buyForm.temp.courseName = this.buyForm.courseName;
              this.buyForm.temp.rzCode = this.buyForm.rzCode
              this.buyCourseDialog = false
            }
          });
        })
      },
      gotoDetail(courseId) {
        this.$router.push(`/main/course/${courseId}`);
      },
      uploadBuyImg(url) {
        this.buyForm.buyImg = url;
      },
      goPay(index) {
        let item = this.list[index];
        this.buyForm = Object.assign({}, item)
        this.buyForm.company = this.userInfo.company;
        this.buyForm.temp = item;
        let isEdu = this.type == '施工现场专业人员';

        // 绕开检查
        // this.stage = 2;
        // this.isCompanyPay = 1;
        // this.isCommitment=''
        // this.buyForm.rzCode = item.rzCode;
        // this.buyForm.company = 'ssss';
        // this.buyForm.courseName = item.courseName;
        // this.buyCourseDialog = true;
        // 检查
        httpServer("weixin.docheckpay", {
          courseId: item.course_id
        }).then((res) => {
          if (res.code == 200) {
            this.stage = 2;
            this.isCompanyPay = 0;
            this.isCommitment=''
            Object.assign( this.buyForm, res.data);
            if( isEdu){
              this.buyCourseDialog = true;
            }else{
              this.commitmentDialog = true;
            }
          }
        });
      },
      gotoPlay(courseId) {
        this.$router.push(`/center/class/train`);
      },
      handleCurrentChange(page) {
        this.page = page;
        this.getData()
      },
      login(param) {
        httpServer("Auth.LoginStudy", param).then(res => {
          if (res.code == 200) {
            let {
              nickname,
              token,
              uid
            } = res.data;
            localStorage.nickname = nickname;
            localStorage.token = token;
            localStorage.uid = uid
            this.user = {
              nickname,
              token,
              uid
            };
            this.loginOk = true
            this.loginDialog = false
          }
        })
      }
    },
  };
</script>
<style>
  @import "../../../assets/css/content.css";
  @import "../../../assets/css/course.css";
  @import url("./index.css");

  .buyForm-title {
    width: 90px;
    padding: 2px;
    text-align: center;
  }

  .commitment-header {
    font-size: 16px;
    text-align: left;
  }

  .commitment {
    text-indent: 30px;
    margin-top: 20px;
    font-size: 16px;
    line-height: 200%;
  }

  .commitment-footer {
    margin-top: 30px;
    text-indent: 50px;
    font-size: 16px;
    text-align: left;
  }

  .commitment-content {
    font-size: 16px;
  }
</style>
