<template>
  <el-dialog
    :title="controlWindows.addInfo.title"
    width="50%"
    :visible.sync="controlWindows.add"
    @opened="openDrawer"
    :before-close="closeWindow"
  >
    <div class="add">
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-row :gutter="2">
          <el-col :span="12">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="form.name" placeholder="企业名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业简称" prop="abbreviaName">
              <el-input
                v-model="form.abbreviaName"
                placeholder="企业简称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司级别" prop="parentMark">
              <el-select v-model="form.parentMark" placeholder="请选择">
                <el-option label="主体公司" :value="0" />
                <el-option label="分公司" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.parentMark === 1" :span="12">
            <el-form-item label="上级公司" prop="parentId">
              <!-- 企业名称 -->
              <autocomplete
                class="mr20"
                :params="form"
                :config="configAutocomplete"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业性质" prop="companyNature">
              <el-select v-model="form.companyNature" placeholder="请选择">
                <el-option
                  v-for="(item, index) in companyNatureTypeEnum"
                  :key="index"
                  :label="item.label"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户类型" prop="companyType">
              <el-select v-model="form.companyType" placeholder="请选择">
                <el-option label="油批账户" value="4" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业负责人" prop="businessLeader">
              <!-- 企业名称 -->
              <autocomplete
                class="mr20"
                :params="form"
                :config="configAutocompleteLeader"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司对接类型" prop="companyDockType">
              <el-select v-model="form.companyDockType" placeholder="请选择">
                <el-option label="对接客户" :value="0" />
                <el-option label="平台客户" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24" style="text-align: right">
          <el-button @click="closeWindow">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </el-col>
      </el-form>
    </div>
  </el-dialog>
</template>
<script>
import serve from "api/customerManagement/companyManagement.js";
import commonServe from "api/common.js";

import autocomplete from "components/autocomplete/index.vue";
export default {
  components: {
    autocomplete,
  },
  props: {
    controlWindows: Object,
    companyNatureTypeEnum: Array,
  },
  data() {
    let validatorParentId = (rule, value, callback) => {
      if (this.form.parentId) callback();
      else callback("请选择企业");
    };
    return {
      configAutocomplete: {
        serveTarget: commonServe.getRefineryCompanyList,
        autocompleteKey: "name",
        labelKey: "name",
        valueKey: "parentId",
        placeholder: "上级公司",
        querykey: "parentId",
        echoId: "",
        echoName: "",
      },
      configAutocompleteLeader: {
        serveTarget: serve.liekQuery,
        labelKey: "nickName",
        valueKey: "id",
        placeholder: "企业负责人",
        querykey: "businessLeader",
        echoId: "",
        echoName: "",
      },
      form: {},
      rules: {
        name: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        abbreviaName: [
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],
        parentMark: [
          { required: true, message: "请选择是否主体公司", trigger: "change" },
        ],
        parentId: [
          { required: true, validator: validatorParentId, trigger: "change" },
        ],
        companyNature: [
          { required: true, message: "请选择企业性质", trigger: "change" },
        ],
        companyType: [
          { required: true, message: "请选择账户类型", trigger: "change" },
        ],
      },
    };
  },
  created() {},
  methods: {
    openDrawer() {
      let { id } = this.controlWindows.addInfo;
      if (id) {
        //回显
        this.form = JSON.parse(JSON.stringify(this.controlWindows.addInfo));
      }
      // 上级公司
      let configUpdata = {
        echoId: this.form.parentId || "",
        echoName: this.form.name || "",
      };
      // 企业负责人
      let configUpdataLeader = {
        echoId: this.form.businessLeader || "",
        echoName: this.form.businessLeader || "",
      };
      Object.assign(this.configAutocomplete, configUpdata);
      Object.assign(this.configAutocompleteLeader, configUpdataLeader);
    },

    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let params = {
            enableMark: 1,
            auditMark: 0,
            createSource: "REFINERY_OMS_WEB",
            ...this.form,
          };
          this.judgeInterface(params).then((res) => {
            if (res.code === 20000) {
              this.$message.success(res.msg);
              this.closeWindow();
            }
          });
        }
      });
    },
    judgeInterface(form) {
      let { id } = this.controlWindows.addInfo;
      if (id) return serve.update(form);
      else return serve.save(form);
    },

    closeWindow() {
      this.$emit("closeWindow");
      this.form = {};
      this.controlWindows.addInfo = {};
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
        this.controlWindows.add = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.add {
  padding: 20px;
  .el-input,
  .el-select,
  .el-textarea,
  .el-autocomplete {
    width: 230px;
  }
}
</style>
