<template>
  <div id="businessLevel">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <table-list
      rowKey="id"
      ref="tableList"
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
      @editInfo="editInfo"
      @sortFunc="sortFunc"
      @editName="editName"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 0)"
          >修改</el-button
        >
        <el-button type="text" @click="del(props.scope.row)">删除</el-button>
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="formData.pageSize"
      :currentPage="formData.pageNum"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      width="560px"
      :show-close="false"
      @closed="loadingClose"
      :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="110px"
          :model="listData"
          :rules="rules"
          ref="listData"
        >
          <el-form-item
            v-for="(items, indexs) in listitem"
            :key="indexs"
            :label="items.label"
            :prop="items.prop"
          >
            <el-radio-group
              v-if="items.scope === 'status'"
              v-model="listData[items.prop]"
            >
              <el-radio
                v-for="(item, index) in items.options"
                :key="index"
                :label="item.value"
                :disabled="statusPop === 2"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
            <el-select
              v-model="listData[items.prop]"
              filterable
              :disabled="statusPop === 0"
              placeholder="请选择教育类型"
              v-else-if="items.prop === 'educationId'"
              @change="changeJYList"
            >
              <el-option
                v-for="item in optionsSF"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="listData[items.prop]"
              filterable
              :disabled="statusPop === 0"
              placeholder="请选择项目类型"
              v-else-if="items.prop === 'projectId'"
              @change="changeXMList(listData)"
            >
              <el-option
                v-for="item in optionsXM"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-input
              :disabled="statusPop === 2"
              v-else-if="items.scope === 'textarea'"
              type="textarea"
              v-model="listData[items.prop]"
            ></el-input>
            <el-input-number
              :controls="false"
              v-else-if="items.scope === 'numType'"
              v-model="listData[items.prop]"
              controls-position="right"
              :precision="1"
              :min="0"
            ></el-input-number>
            <el-input-number
              :disabled="statusPop === 2"
              v-else-if="items.scope === 'NumberSort'"
              v-model="listData[items.prop]"
              :min="0"
              style="width: 100px"
              :controls="false"
            ></el-input-number>
            <el-input
              :disabled="statusPop === 2"
              v-else
              v-model="listData[items.prop]"
            ></el-input>
          </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>
  </div>
</template>

<script>
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name: "BusinessLevel",
  components: { searchBoxNew, tableList, pagination },
  data() {
    return {
      disabledBtn: false,
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "业务层次",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: false,
        openCheckMore: true,
        changeWidth: "130px",
        backFatherBtn: {
          status: false,
          title: "配置下单填选模板",
        },
      },
      //搜索
      formList: [
        {
          prop: "educationId",
          placeholder: "教育类型",
          scope: "educationType",
        },
        {
          prop: "projectId",
          placeholder: "项目类型",
          scope: "projectLevel",
        },
        {
          prop: "status",
          placeholder: "状态",
          scope: "select",
          noClear: false,
          options: [
            {
              label: "全部",
              value: "0,1",
            },
            {
              label: "启用",
              value: 1,
            },
            {
              label: "关闭",
              value: 0,
            },
          ],
        },
        {
          prop: "businessName",
          placeholder: "请输入业务层次名称",
        },
      ],
      formData: {
        status: "0,1",
        pageSize: 10,
        pageNum: 1,
      },
      // 表单
      tableSet: [
        {
          label: "排序",
          prop: "sort",
          scope: "sortType",
          hidden: true,
          width: "100px",
        },
        {
          label: "业务层次编码",
          prop: "encoder",
          hidden: true,
        },
        {
          label: "业务层次名称",
          prop1: "projectName",
          prop2: "businessName",
          scope: "InfoMore",
          hidden: true,
        },
        {
          label: "(旧系统)业务层次名称",
          prop: "oldBusinessName",
          hidden: true,
        },
        {
          label: "业务层次别名",
          prop: "aliasName",
          hidden: true,
          scope: "editName",
        },
        {
          label: "教育类型",
          prop: "educationName",
          hidden: true,
        },
        {
          label: "状态",
          prop: "status",
          hidden: true,
          scope: "status",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      // 弹窗字段
      listitem: [
        {
          label: "教育类型",
          prop: "educationId",
          scope: "select",
        },
        {
          label: "项目类型",
          prop: "projectId",
          scope: "select",
        },
        {
          label: "业务层次名称",
          prop: "businessName",
        },
        {
          label: "(旧系统)业务层次名称",
          prop: "oldBusinessName",
        },
        {
          label: "学年",
          prop: "schoolYear",
          scope: "numType",
          ch: "年",
        },
        {
          label: "状态",
          prop: "status",
          scope: "status",
          options: [
            {
              label: "启用",
              value: 1,
            },
            {
              label: "关闭",
              value: 0,
            },
          ],
        },
        {
          label: "排序",
          prop: "sort",
          scope: "NumberSort",
        },
        {
          label: "备注",
          prop: "remark",
          scope: "textarea",
        },
      ],
      //   弹窗数据
      listData: {},
      statusPop: -1,
      dialogVisible: false,
      optionsSF: [], //教育类型
      optionsXM: [], //项目类型
      //表单验证
      rules: {
        educationId: [
          {
            required: true,
            message: "请选择教育类型",
            trigger: ["blur", "change"],
          },
        ],
        projectId: [
          {
            required: true,
            message: "请选择项目类型",
            trigger: ["blur", "change"],
          },
        ],
        businessName: [
          { required: true, message: "请输入业务层次名称", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        schoolYear: [{ required: false, message: "学年不能为空" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  mounted() {
    this.search();
  },
  activated() {
    this.search();
  },
  methods: {
    /**
     * 修改别名
     */
    editName(item) {
      this.$prompt("请输入新别名", "修改", {
        inputErrorMessage: "请输入内容",
        inputValidator: (value) => {
          if (value == null || value == "" || value == undefined) {
            return false;
          } else {
            if (value.trim().length == 0) {
              return false;
            } else {
              return true;
            }
          }
        },
        inputValue: item.aliasName,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            let apiData = JSON.parse(JSON.stringify(item));
            apiData.aliasName = instance.inputValue;
            this.$api.editCourseBusiness(apiData).then((res) => {
              done();
              this.$store.commit("BUSINESSLEVEL");
              this.search();
            });
          } else {
            done();
          }
        },
      })
        .then(({ value }) => {
          this.$message.success("修改成功");
        })
        .catch(() => {});
      console.log(item);
    },
    sortFunc(row) {
      if (row.sort || row.sort == 0) {
        this.$api.editCourseBusiness(row).then((res) => {
          this.$message.success("修改成功");
          this.$store.commit("BUSINESSLEVEL");
          this.search();
        });
      } else {
        this.$message.warning("排序有误，请重新输入");
        return;
      }
    },
    changeXMList(option) {
      if (!option.educationId) {
        this.optionsXM.map((item, index) => {
          if (item.value === option.projectId) {
            this.listData.educationId = item.pid;
          }
        });
      }
    },
    changeJYList() {
      if (this.listData.projectId) {
        this.listData.projectId = "";
        this.listData.projectName = "";
      }
      this.getXMType(this.listData.educationId);
    },
    editInfo(v) {
      this.addClick(v, 0);
    },
    getEduType() {
      this.$api.inquireCourseEducationType({ status: 1 }).then((res) => {
        var arrays = [];
        res.rows.map((item, index) => {
          arrays.push({
            label: item.educationName,
            value: item.id,
          });
        });
        this.optionsSF = arrays;
      });
    },
    loadingClose() {
      this.disabledBtn = false;
    },
    getXMType(id) {
      this.$api
        .inquireCourseProjectType({
          status: 1,
          educationId: id ? id : undefined,
        })
        .then((res) => {
          var arrays = [];
          res.rows.map((item, index) => {
            arrays.push({
              label: item.projectName,
              value: item.id,
              pid: item.educationId,
            });
          });
          this.optionsXM = arrays;
        });
    },
    search(v) {
      this.loading = true;
      if (v === 2) {
        this.formData = {
          status: "0,1",
          pageSize: 10,
          pageNum: 1,
        };
      }
      var data = JSON.parse(JSON.stringify(this.formData));
      this.$api
        .inquirebusinessList(data)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
    del(v) {
      this.$alert(
        "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
        "提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          var data = {
            id: v.id,
            status: -1,
          };
          this.$api.editCourseBusiness(data).then((res) => {
            this.$message.success("删除成功");
            this.$refs.tableList.clearMoreActive();
            this.$store.commit("BUSINESSLEVEL");
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addClick(v, int) {
      this.getEduType();
      this.getXMType();
      if (v === undefined) {
        this.statusPop = 1;
        this.listData = {
          status: 1,
        };
      } else {
        this.statusPop = int;
        this.listData = JSON.parse(JSON.stringify(v));
      }
      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;
      if (this.statusPop === 1) {
        this.$api
          .appCourseBusiness(this.listData)
          .then((res) => {
            this.$message.success("新增成功");
            this.dialogVisible = false;
            this.$store.commit("BUSINESSLEVEL");
            this.search();
          })
          .catch(() => {
            this.disabledBtn = false;
          });
      }
      if (this.statusPop === 0) {
        this.$api
          .editCourseBusiness(this.listData)
          .then((res) => {
            this.$message.success("修改成功");
            this.dialogVisible = false;
            this.$store.commit("BUSINESSLEVEL");
            this.search();
          })
          .catch(() => {
            this.disabledBtn = false;
          });
      }
    },
    close() {
      this.dialogVisible = false;
    },
    handleSizeChange(v) {
      this.formData.pageSize = v;
      this.formData.pageNum = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.formData.pageNum = 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;
  }
}
.numInputs {
  width: 150px;
}
</style>

