<template>
  <div>
    <BaseDialog
      width="1400px"
      :isShow.sync="isShow"
      :title="title"
      @close="close"
      @submit="submitForm"
    >
      <div class="bop-tip">
        <div><i class="el-icon-warning-outline"></i>说明</div>
        <div>
          ●
          如果提成方式是按阶梯价计算，则需设置阶梯价格区间（最高价无穷大，则填写
          “ * ” ）。如果无需按阶梯价计算提成，留空即可。
        </div>
      </div>
      <el-form
        inline
        hide-required-asterisk
        :model="form"
        :rules="rules"
        ref="form"
      >
        <div>
          <el-form-item prop="tpName" label="模板名称：" label-width="100px">
            <el-input
              placeholder="请输入模板名称"
              v-model="form.tpName"
            ></el-input>
          </el-form-item>
          <el-form-item prop="tenantId" label="">
            <el-select v-model="form.tenantId" placeholder="请选择关联机构">
              <el-option
                v-for="item in companyList"
                :key="item.tenantId"
                :label="item.tenantName"
                :value="item.tenantId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-checkbox
              :true-label="1"
              :false-label="0"
              v-model="form.defaultStatus"
              >默认</el-checkbox
            >
          </el-form-item>
        </div>

        <div v-for="(item, index) in form.itemList" :key="index">
          <el-form-item
            :prop="'itemList.' + index + '.itemName'"
            :label="index == 0 ? '成本设置：' : ' '"
            :rules="rules['itemName']"
            label-width="100px"
          >
            <el-input
              placeholder="请输入成本项名称"
              v-model="item.itemName"
            ></el-input>
          </el-form-item>
          <el-form-item
            label=""
            :prop="'itemList.' + index + '.itemCategory'"
            :rules="rules['itemCategory']"
          >
            <el-select v-model="item.itemCategory" placeholder="请选择成本类型">
              <el-option label="业务类型" :value="1"></el-option>
              <el-option label="非分成" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <template v-if="item.itemCategory == 1">
            <el-form-item
              label=""
              :prop="'itemList.' + index + '.educationTypeId'"
              :rules="rules['educationTypeId']"
            >
              <el-select
                @change="changeEdu(item)"
                v-model="item.educationTypeId"
                placeholder="请选择教育类型"
              >
                <el-option
                  v-for="item in eduList"
                  :key="item.id"
                  :label="
                    item.schemeName +
                    (item.schemeName ? '-' : '') +
                    item.educationName
                  "
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label=""
              :prop="'itemList.' + index + '.businessId'"
              :rules="rules['businessId']"
            >
              <el-select v-model="item.businessId" placeholder="请选择业务层次">
                <el-option
                  v-for="level in backbusinessList(item.educationTypeId)"
                  :key="level.businessId"
                  :label="level.aliasName"
                  :value="level.businessId"
                  @click.native="changeBus(item, level.projectId)"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </template>
          <el-form-item
            label=""
            :prop="'itemList.' + index + '.itemType'"
            :rules="rules['itemType']"
          >
            <el-select v-model="item.itemType" placeholder="请选择成本类型">
              <el-option label="百分比成本" :value="1"></el-option>
              <el-option label="固定成本" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label=""
            :prop="'itemList.' + index + '.email2'"
            :rules="rules['email2']"
            class="range"
          >
            <el-col :span="11">
              <el-input placeholder="最低价" v-model="item.minValue"></el-input>
            </el-col>
            <el-col class="line" :span="2" style="magrin: 10px">-</el-col>
            <el-col :span="11">
              <el-input placeholder="最高价" v-model="item.maxValue"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item
            class="ddd"
            v-if="item.itemType == 1"
            label-width="0"
            :prop="'itemList.' + index + '.typeValue'"
            label=" "
            :rules="rules['typeValue']"
          >
            <el-input placeholder="百分比成本" v-model="item.typeValue">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item
            v-if="item.itemType == 2"
            class="ddd"
            label-width="0"
            :prop="'itemList.' + index + '.typeValue'"
            label=" "
            :rules="rules['typeValue']"
          >
            <el-input placeholder="固定成本" v-model="item.typeValue">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label-width="0" label=" ">
            <div class="btns">
              <!-- <el-button type="text">复制</el-button> -->
              <i @click="add(index, item)" class="el-icon-connection"></i>
              <i @click="add(index)" class="el-icon-circle-plus-outline"></i>
              <i
                v-if="index != 0"
                @click="del(index)"
                class="el-icon-remove-outline"
              ></i>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
import { eduList, addCost, costDetail, editCost } from "@/api/financed/index";
export default {
  name: "DislogSet",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    // 0 新增  1修改  2复制
    type: {
      type: [String, Number],
      default: 0,
    },
    tpId: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      form: {},
      rules: {
        tpName: [
          { required: true, message: "请输入模板名称", trigger: "blur" },
        ],
        tenantId: [
          { required: true, message: "请选择关联机构", trigger: "change" },
        ],
        itemName: [
          { required: true, message: "请输入成本项名称", trigger: "blur" },
        ],
        businessId: [
          { required: true, message: "请选择业务层次", trigger: "blur" },
        ],
        educationTypeId: [
          { required: true, message: "请选择教育类型", trigger: "change" },
        ],
        itemCategory: [
          { required: true, message: "请选择业务类型", trigger: "change" },
        ],
        itemType: [
          { required: true, message: "请选择成本类型", trigger: "change" },
        ],
        email3: [
          { required: true, message: "请输入固定成本", trigger: "blur" },
        ],
        typeValue: [
          { required: true, message: "请输入百分比成本", trigger: "blur" },
        ],
      },
      companyList: [],
      eduList: [],
    };
  },
  methods: {
    init() {
      this.resetForm();
      this.tpId && this.getCostDetail();
      this.getCompanyList();
      this.getEduList();
    },
    getCostDetail() {
      costDetail(this.tpId).then((res) => {
        if (this.type == 2) delete res.data["tpId"];
        Object.keys(this.form).map((key) => {
          this.form[key] = res.data[key];
        });
      });
    },
    getEduList() {
      if (this.eduList.length) return;
      eduList({}).then((res) => {
        this.eduList = res.rows;
      });
    },
    backbusinessList(eduId) {
      if (!eduId || !this.eduList.length) return [];
      let data = this.eduList.find((e) => e.id == eduId);
      return data.businessList || [];
    },
    getCompanyList() {
      if (this.companyList.length) return;
      this.$api.systemtenantlist(this.formData).then((res) => {
        this.companyList = res.rows;
      });
    },
    changeEdu(data) {
      data.businessId = undefined;
      data.projectId = undefined;
    },
    changeBus(data, projectId) {
      data.projectId = projectId;
    },
    add(index, data) {
      data = data || {
        email: undefined,
        businessId: undefined,
        email2: undefined,
        email3: undefined,
        email4: undefined,
      };
      this.form.itemList.splice(index + 1, 0, data);
    },
    del(index) {
      this.form.itemList.splice(index, 1);
    },
    close() {
      this.$refs["form"].resetFields();
    },
    changeEduType() {},
    submit() {},
    resetForm() {
      this.form = {
        itemList: [
          {
            projectId: undefined,
            itemName: 456,
            itemCategory: "1",
            businessId: undefined,
            educationTypeId: 21,
            itemType: "1",
            typeValue: undefined,
            minValue: undefined,
            maxValue: undefined,
          },
        ],
        tpId: undefined,
        tpName: 123,
        defaultStatus: 0,
        tenantId: "1",
      };
    },
    cb() {
      this.$message.success(this.title + "成功");
      this.isShow = false;
      this.$emit("search");
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.type !== 1) {
            addCost(this.form).then(this.cb);
          } else {
            editCost(this.form).then(this.cb);
          }
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        if (this.dialogVisible) {
          this.init();
        }
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
    title() {
      return ["新增", "修改", "复制"][this.type];
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/.el-input--medium .el-input__inner {
  width: 144px;
}
/deep/ .range {
  .el-form-item__content {
    width: 170px;
  }
  .el-input--medium .el-input__inner {
    width: 76px;
  }
}
/deep/ .ddd {
  margin-left: -10px;
  .el-input--medium .el-input__inner {
    width: 105px;
  }
}
.line {
  text-align: center;
}
.btns {
  height: 36px;
  display: flex;
  align-items: center;
  i {
    font-size: 24px;
    cursor: pointer;
    margin-left: 5px;
  }
}
.bop-tip {
  background: #fff6f7;
  margin: 0 0 20px 10px;
  padding: 8px 10px;
  i {
    font-size: 16px;
    color: #e6a23c;
  }
}
</style>
