<script setup>
import { ref } from "vue";

import { raiInterface } from "@/api/api.js";
import { ElMessageBox, ElMessage } from "element-plus";

const props = defineProps({
  addIndustryDlg: {
    type: Boolean,
    default: false,
  },
  addMarkDlg: {
    type: Boolean,
    default: false,
  },
  optionFormregion: {
    type: String,
  },
  addMarkUpVal: {
    type: Object,
    default: {},
  },
  source: {
    type: Number,
    required: true,
  },
});

const inputIndustry = ref("");
const mark = ref([{ name: "" }]);
const chartPermissionList = ref([]);
const addIndustryForm = ref({
  //添加产业分类
  tradeValue: "",
  industryName: "",
  recommendedIndex: null,
});
const industryAdd = ref({
  industryName: [{ required: true, message: "请输入产业名称（最多输入10个字）", trigger: "blur" }],
  recommendedIndex: [{ required: true, message: "请输入推荐指数", trigger: "blur" }],
  tradeValue: [{ required: true, message: "请选择行业", trigger: "change" }],
});

//获取行业
function chartPermission() {
  raiInterface.chartPermissionFirstHaveIco().then((res) => {
    if (res.Ret === 200) {
      chartPermissionList.value = res.Data.List;
    }
  });
}

const $emit = defineEmits(["commitMarkDlg"]);

const addIndustryRef = ref(null);
//添加行业的确定事件
async function commitIndustry() {
  if (props.optionFormregion != "研选") {
    addIndustryRef.value.validate(async (val) => {
      if (val) {
        //添加产业
        const res = await raiInterface.industrialManagement({
          IndustryName: addIndustryForm.value.industryName,
          RecommendedIndex: addIndustryForm.value.recommendedIndex - 0,
          ChartPermissionId: addIndustryForm.value.tradeValue,
        });
        if (!res.Success) return;
        $emit("commitIndustryDlg", res.Data);
        ElMessage.success("添加成功！");
        confirmIndustry();
      }
    });
  } else {
    if (inputIndustry.value) {
      const res = await raiInterface.industrialManagement({
        Source: props.source,
        IndustryName: inputIndustry.value,
        PermissionName: props.optionFormregion,
      });
      if (res.Ret === 200) {
        $emit("commitIndustryDlg", res.Data);
        ElMessage.success("添加成功！");
        confirmIndustry();
      }
    } else {
      ElMessage.error("请输入产业！");
    }
  }
}
//添加行业的取消事件
function confirmIndustry() {
  inputIndustry.value = "";
  $emit("update:addIndustryDlg", false);
}
//添加标的的确定事件
async function commitMark() {
  let arr = [];
  mark.value.forEach((item) => {
    if (item.name) {
      arr.push(item.name);
    }
  });
  if (!arr.length) returnElMessage.error("请输入标的！");
  const res = await raiInterface.industrialSubjectAdd({
    IndustrialManagementId: props.addMarkUpVal.industrialId,
    Source: props.source,
    SubjectName: arr.join(",").replace(/,/g, "{|}"),
  });
  if (res.Ret === 200) {
    $emit("commitMarkDlg", res.Data.NewId);
    ElMessage.success("添加成功！");
    confirmMark();
  }
}
//添加标的的取消事件
function confirmMark() {
  mark.value = [{ name: "" }];
  $emit("update:addMarkDlg", false);
}
//添加标的的输入框
function addMarkInput() {
  mark.value.push({ name: "" });
}
</script>

<template>
  <div class="industry-mark">
    <!-- 添加行业 -->
    <el-dialog
      draggable
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      center
      title="添加产业"
      v-model="props.addIndustryDlg"
      customClass="custom-addIndustryMark"
      :before-close="confirmIndustry"
    >
      <div v-if="props.optionFormregion === '研选'">
        <p>所属行业：{{ props.optionFormregion }}</p>
        <el-input v-model="inputIndustry" maxlength="10" placeholder="请输入产业名称（最多输入10个字）"></el-input>
      </div>
      <div v-else>
        <el-form :model="addIndustryForm" :rules="industryAdd" ref="addIndustryRef">
          <el-form-item prop="tradeValue">
            <el-select placeholder="请选择行业" style="width: 100%" clearable v-model="addIndustryForm.tradeValue" @change="changeHandel" @focus="chartPermission">
              <el-option v-for="item in chartPermissionList" :label="item.PermissionName" :key="item.ChartPermissionId" :value="item.ChartPermissionId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="industryName">
            <el-input style="width: 100%" placeholder="请输入产业名称（最多输入10个字）" v-model="addIndustryForm.industryName" maxlength="10"></el-input>
          </el-form-item>
          <el-form-item prop="recommendedIndex">
            <el-input style="width: 100%; margin-top: 20px" placeholder="请设置推荐指数（0-100）" v-model="addIndustryForm.recommendedIndex"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="commitIndustry">确定</el-button>
          <el-button @click="confirmIndustry">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 添加标的 -->
    <el-dialog
      draggable
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      center
      title="添加标的"
      v-model="props.addMarkDlg"
      customClass="custom-addIndustryMark"
      :before-close="confirmMark"
    >
      <div>
        <p>所属产业：{{ props.addMarkUpVal.industrialName }}</p>
        <div v-for="(item, index) in mark" :key="index">
          <el-input v-model="item.name" maxlength="6" placeholder="请输入标的名称（最多输入6个字）"></el-input>
        </div>
        <div class="add-mark" @click="addMarkInput"><img src="~@/assets/img/set_m/add_ico.png" /> 继续添加</div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="commitMark">确定</el-button>
          <el-button @click="confirmMark">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
.industry-mark {
  .custom-addIndustryMark {
    width: 500px;
    .el-dialog__footer {
      margin-top: 20px;
    }
  }
  .el-input {
    width: 100%;
    margin-top: 20px;
  }
  .add-mark {
    display: flex;
    align-items: center;
    margin-top: 20px;
    color: #409eff;
    img {
      margin-right: 15px;
    }
  }
}
</style>
