<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :rules="rules" label-width="108px">
      <el-card>
        <div slot="header" class="clearfix">
          <span>选择分配学校</span>
        </div>
        <div style="position: relative">
          <el-form-item label="分配代理商" prop="agentId">
            <el-select v-model="form.agentId" placeholder="请选择分配代理商" clearable filterable size="small">
              <el-option v-for="dict in allAgentList" :key="dict.agentId" :label="dict.name" :value="dict.agentId"/>
            </el-select>
          </el-form-item>
          <el-form-item label="分配省份" prop="distributeProvinceId">
            <el-select v-model="form.distributeProvinceId" placeholder="请选择分配省份" clearable filterable size="small" @change="onProvinceChanged">
              <el-option v-for="pro in provinceList" :key="pro.id" :label="pro.sname" :value="pro.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="分配学校" prop="distributeSchoolId">
            <el-select v-model="form.distributeSchoolId" :remote-method="getSchoolByProvince" placeholder="请选择分配学校" clearable filterable size="small">
              <el-option v-for="scl in schoolList" :key="scl.id" :label="scl.name" :value="scl.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="分配考生类别" prop="distributeExamType">
            <el-select v-model="form.distributeExamType" placeholder="请选择分配考生类别" clearable filterable size="small">
              <el-option v-for="t in examTypes" :key="t" :label="t" :value="t"/>
            </el-select>
          </el-form-item>
          <div style="position: absolute;top: 0;left: 400px;width: 700px;word-wrap:break-word">{{msgSuccessStr}}</div>
        </div>
      </el-card>
      <el-card style="margin-top: 5px">
        <div slot="header" class="clearfix">
          <span>填写分配卡段</span>
        </div>
        <el-form-item label="卡类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择卡类型" clearable>
            <el-option
              v-for="dict in dict.type.card_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="起始账号" prop="startCard">
          <el-input v-model="form.startCard" placeholder="请输入起始账号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="截止账号" prop="endCard">
          <el-input v-model="form.endCard" placeholder="请输入截止账号" style="width: 200px"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="distributionCard(0)">分配</el-button>
          <el-button type="primary" @click="distributionCard(1)">重新分配</el-button>
        </el-form-item>

      </el-card>
    </el-form>
    <el-card style="margin-top: 5px">
      请先选择需要分配的对象，然后填写分配的卡区间
    </el-card>
  </div>
</template>

<script>
  import {distributionCards} from "@/api/voluntary/busiCards";
  import {getAllAgents} from "@/api/voluntary/busiAgents";
  import {locationList} from "@/api/voluntary/sysLocations";
  import {getAllBusiSchools} from "@/api/voluntary/busiSchools";
  import {getExamTypes} from "../../../api/voluntary/busiCards";

  export default {
    name: "BusiDistribution",
    dicts: [ 'card_type'],
    components: {},
    data() {
      return {
        msgSuccessStr: null,
        allAgentList: [],
        provinceList: [],
        schoolList: [],
        examTypes: [],
        form: {
          distributeProvinceId: '',
          distributeSchoolId: '',
          distributeExamType: '',
          startCard: '',
          endCard: '',
          agentId: '',
          agentName: '',
          type: null
        },
        // 表单校验
        rules: {
          startCard: [
            {required: true, message: "起始账号不能为空", trigger: "blur"}
          ],
          endCard: [
            {required: true, message: "截止账号不能为空", trigger: "blur"}
          ],
          agentId: [
            {required: true, message: "分配对象不能为空", trigger: "change"}
          ],
          type: [
            {required: true, message: "卡类型不能为空", trigger: "change"}
          ]

        },
      };
    },
    created() {
      // 获取所有代理商
      this.getAllAgents();
      // 所有省份
      this.getAllProvinces()
      // 所有考生类别
      this.getAllExamTypes()
    },
    methods: {
      async getAllAgents() {
        const res = await getAllAgents()
        this.allAgentList = res.rows
      },
      async getAllProvinces() {
        const res = await locationList({level: 1})
        this.provinceList = res.data
      },
      async getAllExamTypes() {
        const res = await getExamTypes()
        this.examTypes = res.data
      },
      async getSchoolByProvince(name) {
        if (!this.form.distributeProvinceId) return
        const res = await getAllBusiSchools({pro: this.form.distributeProvinceId, name})
        this.schoolList = res.data
      },
      async onProvinceChanged() {
        this.schoolList = []
        this.form.distributeSchoolId = ''
        await this.getSchoolByProvince()
      },
      async distributionCard(againDistributionCard) {
        var startCardNo = this.form.startCard
        var endCardNo = this.form.endCard
        var msg = '是否确认分配[' + (startCardNo - endCardNo + 1) + ']张卡：' + startCardNo + '-' + endCardNo

        await this.$refs["form"].validate()

        await this.$confirm(msg, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })

        const response = await distributionCards({...this.form, againDistributionCard})
        console.log(response.msg)
        this.$modal.msgSuccess("分配成功");
        this.msgSuccessStr = response.msg;
      }
    }
  };
</script>
