<template>
  <div id="qianpeiArrange">
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
      @editInfo="editInfo"
    >
      <template slot="btn" slot-scope="props">
        <el-button
          type="text"
          @click="addClick(props.scope.row, 0)"
          :disabled="props.scope.row.status === 1"
          >修改计划</el-button
        >
        <el-button
          type="text"
          @click="editGoods(props.scope.row)"
          :disabled="props.scope.row.status === 1"
          >适用商品</el-button
        >
        <el-button type="text" @click="watchs(props.scope.row)">预览</el-button>
        <el-button
          type="text"
          @click="del(props.scope.row, 1)"
          v-if="props.scope.row.status === 0"
          >启用</el-button
        >
        <el-button
          type="text"
          @click="del(props.scope.row, 2)"
          v-if="props.scope.row.status === 1"
          >关闭</el-button
        >
        <el-button type="text" @click="del(props.scope.row, 3)">删除</el-button>
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      @closed="loadingClose"
      :visible.sync="dialogVisible"
      width="610px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ statusPop === 1 ? "添加" : statusPop === 0 ? "修改" : "详情" }}
        </div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="close" />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="150px"
          :model="listData"
          :rules="rules"
          ref="listData"
        >
          <el-form-item label="前培标题" prop="beforeName">
            <el-input
              :disabled="statusPop === 2"
              v-model="listData.beforeName"
            ></el-input>
          </el-form-item>
          <el-form-item label="关联题卷" required prop="beforeStatus">
            <el-select v-model="listData.beforeStatus" placeholder="请选择">
              <el-option
                v-for="item in optionsApis"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="beforeUrl" v-if="listData.beforeStatus">
            <el-radio-group v-model="listData.beforeUrl">
              <el-radio
                style="margin-bottom: 10px"
                v-for="(item, index) in radioApi"
                :key="index"
                :label="item.beforeUrlId"
                >{{ item.urlName }} - {{ item.url }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="学习开放时间" required>
            <el-col :span="11">
              <el-form-item prop="beforeStartTime">
                <el-date-picker
                  type="datetime"
                  placeholder="开始日期"
                  @change="changeEndTime(1)"
                  v-model="listData.beforeStartTime"
                  style="width: 100%"
                  value-format="timestamp"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="line" style="text-align: center" :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="beforeEndTime">
                <el-date-picker
                  type="datetime"
                  placeholder="结束日期"
                  @change="changeEndTime(2)"
                  v-model="listData.beforeEndTime"
                  style="width: 100%"
                  value-format="timestamp"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          :loading="disabledBtn"
          v-if="statusPop !== 2"
          @click="submit('listData')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogVisiblenew"
      width="560px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">适用商品</div>
        <div class="rightBoxs">
          <img
            src="@/assets/images/Close@2x.png"
            alt=""
            @click="dialogVisiblenew = false"
          />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="150px"
          :model="listDataGoods"
          :rules="rulesnew"
          ref="listDataGoods"
        >
          <el-form-item label="商品类型" prop="goodsType">
            <el-select
              v-model="listDataGoods.goodsType"
              placeholder="请选择"
              disabled
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务层级" prop="educationTypeId">
            <el-select
              v-model="listDataGoods.educationTypeId"
              placeholder="选择教育类型"
              @change="listDataGoods.businessId = ''"
              :disabled="statusPop === 0"
            >
              <el-option
                v-for="item in educationType"
                :key="item.id"
                :label="item.educationName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label=""
            prop="businessId"
            v-if="listDataGoods.educationTypeId"
          >
            <el-select
              v-model="listDataGoods.businessId"
              :disabled="statusPop === 0"
              placeholder="选择业务层次"
            >
              <el-option
                v-for="item in businessList"
                :key="item.id"
                :label="item.projectName + '-' + item.businessName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="适用商品" prop="goodsId">
            <span v-if="!listDataGoods.businessId" style="color: #f56c6c"
              >请先选择业务层级</span
            >
            <el-checkbox-group
              style="overflow: auto; max-height: 500px"
              v-model="listDataGoods.goodsId"
              v-else-if="listDataGoods.businessId && goodsList.length"
            >
              <el-checkbox
                v-for="(item, index) in goodsList"
                :key="index"
                :label="item.goodsId"
                :disabled="
                  item.goodsStatus === 0 &&
                  bfListData.goodsId.indexOf(item.goodsId) === -1
                "
                >{{ item.code + "-" + item.goodsName + "-" }}
                <span
                  :style="
                    item.goodsStatus === 0 &&
                    bfListData.goodsId.indexOf(item.goodsId) === -1
                      ? ''
                      : 'color:#F56C6C'
                  "
                  >￥{{ item.standPrice }}</span
                ></el-checkbox
              >
            </el-checkbox-group>
            <span style="color: #f56c6c" v-else>该业务层级下，无适用商品</span>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblenew = false">取 消</el-button>
        <el-button
          type="primary"
          v-if="statusPop !== 2"
          @click="submitnew('listDataGoods')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="dialoginfoWatch"
      width="860px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">【{{ infoData.beforeName }}】预览</div>
        <div class="rightBoxs">
          <img
            src="@/assets/images/Close@2x.png"
            alt=""
            @click="dialoginfoWatch = false"
          />
        </div>
      </div>
      <div>
        <el-form :model="infoData" class="demo-form-inline" label-width="120px">
          <el-button type="info" style="margin-bottom: 20px" size="mini"
            >前培计划信息</el-button
          >
          <el-form-item label="前培标题：">
            <span>{{ infoData.beforeName }}</span>
          </el-form-item>
          <el-form-item label="关联题卷：">
            <span v-for="(item, index) in optionsApis" :key="index">
              <span v-if="item.value === infoData.beforeStatus">{{
                item.label
              }}</span>
            </span>
            <span>{{ infoData.urlName }}</span> <span>{{ infoData.url }}</span>
          </el-form-item>
          <el-form-item label="开放时间：">
            <span>{{ $methodsTools.onlyForma(infoData.beforeStartTime) }}</span>
            至
            <span>{{ $methodsTools.onlyForma(infoData.beforeEndTime) }}</span>
          </el-form-item>
          <el-button type="info" style="margin-bottom: 20px" size="mini"
            >适用商品</el-button
          >
          <el-form-item label="商品类型：">
            <span v-for="(item, index) in options" :key="index">{{
              item.value === infoData.goodsType ? item.label : ""
            }}</span>
          </el-form-item>
          <el-form-item label="业务层级：">
            <span
              >{{ infoData.educationName }}-{{ infoData.projectName }}-{{
                infoData.businessName
              }}</span
            >
          </el-form-item>
          <el-form-item label="适用商品：">
            <div v-for="(item, index) in infoData.examNumberGoods" :key="index">
              {{ index + 1 }}.{{ item.code }}-{{ item.goodsName }}-<span
                style="color: #f56c6c"
                >￥{{ item.standPrice }}</span
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialoginfoWatch = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name: "QianpeiArrange",
  components: { searchBox, tableList, pagination },
  data() {
    return {
      disabledBtn: false,
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "前培安排",
        index: 0,
        ch: "条",
        num: true,
        changeWidth: "300px",
        choice: true,
        border: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      // 表单
      tableSet: [
        {
          label: "前培编码",
          prop: "code",
          hidden: true,
        },
        {
          label: "前培标题",
          prop: "beforeName",
          hidden: true,
        },
        {
          label: "报名开放时间",
          prop1: "beforeStartTime",
          prop2: "beforeEndTime",
          hidden: true,
          scope: "TimeLists",
          Diszing: false,
        },
        {
          label: "使用状态",
          prop: "status",
          hidden: true,
          width: "130px",
          scope: "isOptions",
          options: [
            {
              label: "进行中",
              value: 1,
            },
            {
              label: "未启用",
              value: 0,
            },
            {
              label: "删除",
              value: -1,
            },
            {
              label: "已过期",
              value: 2,
            },
          ],
        },
        {
          label: "符合条件的学员（人次）",
          prop: "people",
          hidden: true,
          width: "180px",
          scope: "AjumpPeolpe",
          type: 2,
        },
      ],
      optionsApis: [
        // {
        //   label: "内部接口",
        //   value: 1,
        // },
        {
          label: "外部接口",
          value: 2,
        },
        // {
        //   label: "关联题库资源",
        //   value: 3,
        // },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      //   弹窗数据
      listData: {},
      statusPop: -1,
      dialogVisible: false,
      //表单验证
      rules: {
        beforeName: [
          { required: true, message: "请输入前培标题", trigger: "blur" },
        ],
        beforeStatus: [
          { required: true, message: "请选中关联题卷", trigger: "change" },
        ],
        beforeUrl: [
          { required: true, message: "请选择接口地址", trigger: "change" },
        ],
        beforeStartTime: [
          {
            type: "date",
            required: true,
            message: "请选择学习开放开始时间",
            trigger: "change",
          },
        ],
        beforeEndTime: [
          {
            type: "date",
            required: true,
            message: "请选择学习开放结束时间",
            trigger: "change",
          },
        ],
      },
      dialogVisiblenew: false,
      options: [
        {
          label: "视频",
          value: 1,
        },
        {
          label: "题库",
          value: 2,
        },
        {
          label: "补考",
          value: 3,
        },
        {
          label: "前培",
          value: 4,
        },
      ],
      //表单验证
      rulesnew: {
        goodsType: [
          { required: true, message: "请选择商品类型", trigger: "change" },
        ],
        educationTypeId: [
          { required: true, message: "请选择教育类型", trigger: "change" },
        ],
        businessId: [
          { required: true, message: "请选择业务层次", trigger: "change" },
        ],
        goodsId: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个商品",
            trigger: "change",
          },
        ],
      },
      busList: [],
      businessList: [],
      goodsList: [],
      bfListData: {},
      listDataGoods: {},
      radioApi: [],
      dialoginfoWatch: false,
      infoData: {},
    };
  },
  mounted() {
    this.getDict();
    this.search();
  },
  activated() {
    this.getDict();
    this.search();
  },
  watch: {
    "listDataGoods.educationTypeId"(value) {
      this.businessList = this.busList.filter((item) => {
        return item.educationId == value;
      });
    },
    "listDataGoods.businessId"(value) {
      if (value) {
        if (this.statusPop === 1) {
          this.listDataGoods.goodsId = [];
        }
        this.$api
          .inquiresystembeforelistGoods({ businessId: value })
          .then((res) => {
            this.goodsList = res.rows;
          });
      }
    },
    "listData.beforeStatus"(value) {
      this.$api.inquiresystembeforelistUrl({ urlStatus: value }).then((res) => {
        this.radioApi = res.rows;
      });
    },
  },
  computed: { ...mapGetters(["educationType"]) },
  methods: {
    loadingClose() {
      this.disabledBtn = false;
    },
    //修改适用商品
    editGoods(row) {
      this.$api
        .inquiresystembeforelistGoodsgoodsInfo({ beforeId: row.beforeId })
        .then((res) => {
          this.$api
            .inquiresystembeforelistGoods({ businessId: res.data.businessId })
            .then((result) => {
              this.goodsList = result.rows;
            });
          if (res.data.goodsType && res.data.goodsId.length) {
            this.statusPop = 0;
            this.bfListData = JSON.parse(JSON.stringify(res.data));
            this.listDataGoods = JSON.parse(JSON.stringify(res.data));
          } else {
            this.statusPop = 1;
            this.bfListData = {
              goodsId: [],
            };
            this.listDataGoods = {
              beforeId: row.beforeId,
              goodsType: 1,
              businessId: "",
              goodsId: [],
            };
          }
          this.$nextTick(() => {
            this.$refs.listDataGoods.clearValidate();
          });
          this.dialogVisiblenew = true;
        });
    },
    //提交验证适用商品表单
    submitnew(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.rulesTableSumbitnew();
        } else {
          return false;
        }
      });
    },
    //验证通过
    rulesTableSumbitnew() {
      var data = JSON.parse(JSON.stringify(this.listDataGoods));
      this.busList.map((item) => {
        if (item.id === data.businessId) {
          data.projectId = item.projectId;
        }
      });
      this.$api.appsystemaddGoods(data).then((res) => {
        this.$message.success("适用商品设置成功");
        this.dialogVisiblenew = false;
        this.search();
      });
      console.log(data, 321);
    },
    //获取初始基础参数
    getDict() {
      this.$api.inquirebusinessList({ status: 1 }).then((res) => {
        this.busList = res.rows;
      });
    },
    // 判断选择时间逻辑
    changeEndTime(int) {
      if (this.listData.beforeStartTime === this.listData.beforeEndTime) {
        this.$message.warning("开始时间与结束时间不允许相同");
        if (int === 1) {
          this.listData.beforeStartTime = "";
        }
        if (int === 2) {
          this.listData.beforeEndTime = "";
        }
        return;
      }
      if (
        int === 1 &&
        this.listData.beforeEndTime &&
        this.listData.beforeStartTime > this.listData.beforeEndTime
      ) {
        this.$message.warning(
          "当前选择的开始时间大于结束时间，请重新选择开始时间!"
        );
        this.listData.beforeStartTime = "";
        return;
      }
      if (
        int === 2 &&
        this.listData.beforeStartTime &&
        this.listData.beforeEndTime < this.listData.beforeStartTime
      ) {
        this.$message.warning(
          "当前选择的结束时间小于开始时间，请重新选择结束时间!"
        );
        this.listData.beforeEndTime = "";
        return;
      }
    },
    editInfo(v) {
      this.addClick(v, 0);
    },
    search(v) {
      this.loading = true;
      var data = {
        status: "0,1,2",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api
        .inquiresystembefore(data)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search();
    },
    watchs(v) {
      this.$api.obtainsystembefore(v.beforeId).then((res) => {
        this.infoData = res.data;
        this.dialoginfoWatch = true;
      });
    },
    del(v, int) {
      this.$alert(
        `确定${
          int === 1 ? "启用" : int === 2 ? "关闭" : int === 3 ? "删除" : "xxx"
        }前培计划【${v.beforeName}】？<br />${
          int === 1
            ? "启用后，学员端预约考试的入口同步开通，请慎重考虑！"
            : int === 2
            ? "关闭后，学员端预约考试的入口同步关闭，请慎重考虑！"
            : int === 3
            ? "内容删除后将无法恢复，请慎重考虑"
            : "xxx"
        }`,
        "操作提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          var data = {
            beforeId: v.beforeId,
          };
          if (int === 1) {
            data.status = 1;
          }
          if (int === 2) {
            data.status = 0;
          }
          if (int === 3) {
            data.status = -1;
          }
          this.$api.editsystembefore(data).then((res) => {
            this.$message.success(
              `${
                int === 1
                  ? "启用"
                  : int === 2
                  ? "关闭"
                  : int === 3
                  ? "删除"
                  : "xxx"
              }成功`
            );
            this.search();
            this.$store.commit("BEFORELIST");
          });
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除",
          // });
        });
    },
    addClick(v, int) {
      if (v === undefined) {
        this.statusPop = 1;
        this.listData = {};
        this.$nextTick(() => {
          this.$refs.listData.clearValidate();
        });
        this.dialogVisible = true;
      } else {
        this.statusPop = int;
        var res = JSON.parse(JSON.stringify(v));
        if (res.beforeStartTime) {
          res.beforeStartTime = this.$methodsTools.time10to13(
            res.beforeStartTime,
            2
          );
        }
        if (res.beforeEndTime) {
          res.beforeEndTime = this.$methodsTools.time10to13(
            res.beforeEndTime,
            2
          );
        }
        this.listData = res;
        this.$nextTick(() => {
          this.$refs.listData.clearValidate();
        });
        this.dialogVisible = true;
      }
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    rulesTableSumbit() {
      this.disabledBtn = true;
      var data = JSON.parse(JSON.stringify(this.listData));
      data.beforeStartTime = this.$methodsTools.time10to13(
        data.beforeStartTime,
        1
      );
      data.beforeEndTime = this.$methodsTools.time10to13(data.beforeEndTime, 1);
      if (this.statusPop === 1) {
        data.status = 0;
        this.$api
          .appsystembefore(data)
          .then((res) => {
            this.$message.success("新增成功");
            this.dialogVisible = false;
            this.search();
            this.$store.commit("BEFORELIST");
          })
          .catch(() => {
            this.disabledBtn = false;
          });
      }
      if (this.statusPop === 0) {
        this.$api
          .editsystembefore(data)
          .then((res) => {
            this.$message.success("修改成功");
            this.dialogVisible = false;
            this.search();
            this.$store.commit("BEFORELIST");
          })
          .catch(() => {
            this.disabledBtn = false;
          });
      }
    },
    close() {
      this.dialogVisible = false;
    },
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
.dis_fs {
  display: flex;
  align-items: center;
  .imgBoxs {
    width: 200px;
    height: 130px;
    border: 1px solid #999;
    margin-right: 6px;
  }
  .btns {
    align-self: end;
    border: 1px solid #999;
    font-size: 12px;
    height: 28px;
    line-height: 28px;
    padding: 0px 10px;
    border-radius: 4px;
    cursor: pointer;
  }
}
.dis_fs {
  display: flex;
  align-items: center;
}
.jbtw {
  justify-content: space-between;
}
.bors {
  border: 1px solid #999;
  padding: 0px 8px;
  height: 30px;
  line-height: 30px;
  border-radius: 4px;
}
.mar_bgc {
  width: 90%;
  padding: 12px;
  margin-top: 12px;
  background-color: #eee;
}
</style>

