<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="900px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rules"
            size="mini"
            label-width="90px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="行业名称" prop="name">
                  <el-input
                    v-model="ruleForm.name"
                    :disabled="id == '007'"
                    size="mini"
                    placeholder="行业名称"
                    maxlength="20"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="行业资质" prop="need_permits">
                  <el-select
                    style="width: 100%"
                    v-model="ruleForm.need_permits"
                    :disabled="id == '007'"
                    size="mini"
                    placeholder="行业资质"
                  >
                    <el-option label="需要" value="1" />
                    <el-option label="不需要" value="0" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col span="24">
                <el-divider content-position="left">关联数据：</el-divider>
              </el-col>
              <el-col :span="24">
                <el-form-item label="需求项目" prop="item_ids" >
                  <el-checkbox-group
                    style="width: 100%"
                    size="mini"
                    v-model="ruleForm.item_ids"
                    :disabled="id == '007'"
                    placeholder="需求项目"
                  >
                    <el-checkbox
                      v-for="p in actProOptions"
                      :key="p.id"
                      :label="String(p.id)"
                      >{{ p.name }}</el-checkbox
                    >
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="行业标签" prop="tag_ids" >
                  <el-checkbox-group
                    style="width: 100%"
                    v-model="ruleForm.tag_ids"
                    size="mini"
                    :disabled="id == '007'"
                    placeholder="行业标签"
                  >
                    <el-checkbox
                      v-for="p in indTagOptions"
                      :key="p.id"
                      :label="String(p.id)"
                      >{{ p.name }}</el-checkbox
                    >
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="id !== '007'" size="mini" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button  size="mini" @click="showModelThis = false">{{
            id == "007" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/serviceParam/industry";
import asyncRequestTag from "@/apis/service/serviceParam/industryTag";
import asyncRequestPro from "@/apis/service/serviceParam/actProject";
import resToken from "@/mixins/resToken";

/**
 * @props
 * name // 行业名称
 * need_permits // 行业资质
 * item_ids //  关联需求项目
 * tag_ids // 关联行业标签
 */
const form = {
  name: "",
  need_permits: "0",
  item_ids: [],
  tag_ids: [],
};
export default {
  name: "Brand",
  mixins: [resToken],
  props: ["showModel", "id", "sitem"],
  data() {
    return {
      parmValue: {
        page: 1, // 页码
        size: 99999, // 每页显示条数
        name: "",
        status: "1",
      },
      loading: false,
      title: "添加行业",
      showModelThis: this.showModel,
      select: "1",
      actProOptions: [],
      indTagOptions: [],
      activeOptions: [],
      actionList: [],
      ruleForm: {
        name: "",
        need_permits: "0",
        item_ids: [],
        tag_ids: [],
      },
      rules: {
        name: [
          {
            required: true,
            trigger: "blur",
            message: "请输入行业名称",
          },
        ],
        need_permits: [
          {
            required: true,
            trigger: "blur",
            message: "请输入行业资质",
          },
        ],
        item_ids: [
          {
            type: "array",
            required: true,
            trigger: "blur",
            message: "请选择关联需求项目",
          },
        ],
        tag_ids: [
          {
            type: "array",
            required: true,
            trigger: "blur",
            message: "请选择关联行业标签属性",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      const basicTitle = "行业";
      let prefix = this.id === "005" ? "修改" : this.id === "002" ? "添加" : "";
      this.title = prefix ? prefix + basicTitle : basicTitle + "详情";
      this.ruleForm = {
        ...form,
      };
      await this.searchTagList();
      await this.searchProList();
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { id, name, need_permits, item_ids, tag_ids } = this.sitem;
          this.ruleForm = {
            id: id || "",
            name: name || "",
            need_permits: String(need_permits || "0"),
            item_ids: item_ids || [],
            tag_ids: tag_ids || [],
          };
        }
      });
    },
    // 刷新表格
    async searchTagList() {
      const { code, data } = await asyncRequestTag.list(this.parmValue);
      this.indTagOptions = code === 1 ? data.list : [];
    },
    // 刷新表格
    async searchProList() {
      const { code, data } = await asyncRequestPro.list(this.parmValue);
      this.actProOptions = code === 1 ? data.list : [];
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            const model = JSON.parse(JSON.stringify(this.ruleForm));
            let res = {};
            if (this.id === "002") {
              delete model.id;
              res = await asyncRequest.add(model);
            } else {
              res = await asyncRequest.update(model);
            }
            this.loading = false;
            if (res && res.code === 1) {
              const title = this.id === "002" ? "添加成功!" : "修改成功!";
              this.$notify.success({
                title,
                message: "",
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh");
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.brand {
}
</style>
