<template>
  <div class="infoForm">
    <div class="userSelect-input" @click="openDialog">
      <slot></slot>
    </div>
    <el-dialog
      :title="`${type == 'add' ? '新增' : '编辑'}信息`"
      :close-on-click-modal="false"
      :visible.sync="visible"
      lock-scroll
      append-to-body
      width="80%"
      top="10vh"
      :modal-append-to-body="false"
      class="dialog-box"
      destroy-on-close
      v-loading="infoForm_loading"
    >
      <el-form
        :model="infoForm"
        :rules="infoRules"
        ref="infoForm"
        size="small"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="主体企业" prop="companyId">
                <el-select
                  v-model="infoForm.companyId"
                  placeholder="请选择主体企业"
                  clearable
                  v-loadMore="nextCompanyList"
                  style="width: 100%"
                  @change="companyChange"
                >
                  <el-option
                    v-for="item in companyOptions"
                    :key="item.id"
                    :label="item.companyName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <companyForm />
            </el-col>
          </el-col>
          <el-col :span="24" v-if="isShowCompanyInfo">
            <div class="company-info">
              <div class="company-info-items">
                <div :span="12">
                  企业归属：{{ companyInfo.areaName || "--" }}
                </div>
                <div :span="12">
                  联系人：{{ companyInfo.contactUser || "--" }}
                </div>
                <div :span="12">
                  公司地址：{{ companyInfo.address || "--" }}
                </div>
              </div>
              <div class="company-info-items">
                <div :span="12">
                  社会信用代码：{{ companyInfo.socialCreditAgency || "--" }}
                </div>
                <div :span="12">
                  联系电话：{{ companyInfo.contactPhone || "--" }}
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="24">
            <el-form-item label="系统类型" prop="systemType">
              <el-radio-group v-model="infoForm.systemType">
                <el-radio
                  v-for="v in systemTypeOptions"
                  :key="v.Id"
                  :label="v.Id"
                  @change="initSystemClassList"
                  >{{ v.FullName }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统名称" prop="systemName">
              <el-input
                v-model="infoForm.systemName"
                placeholder="请输入系统名称"
                maxlength="50"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统分类" prop="systemClass">
              <el-select
                v-model="infoForm.systemClass"
                placeholder="请选择系统分类"
                :disabled="!infoForm.systemType"
                style="width: 100%"
              >
                <el-option
                  v-for="item in systemClassOptions"
                  :key="item.Id"
                  :label="item.ClassName"
                  :value="item.Id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备案号" prop="systemRecordNumber">
              <el-input
                v-model="infoForm.systemRecordNumber"
                placeholder="请输入备案号"
                maxlength="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备案许可证" prop="recordLicense">
              <el-input
                v-model="infoForm.recordLicense"
                placeholder="请输入备案许可证"
                maxlength="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属平台" prop="platform">
              <el-input
                v-model="infoForm.platform"
                placeholder="请输入归属平台"
                maxlength="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="域名" prop="domain">
              <el-input
                v-model="infoForm.domain"
                placeholder="请输入域名"
                maxlength="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下载量" prop="systemDownloadVolume">
              <el-input
                v-model.number="infoForm.systemDownloadVolume"
                maxlength="200"
                placeholder="请输入下载量"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本" prop="version">
              <el-input
                v-model="infoForm.version"
                maxlength="200"
                placeholder="请输入版本"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人姓名" prop="principalName">
              <el-input
                v-model="infoForm.principalName"
                maxlength="50"
                placeholder="请输入负责人姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人电话" prop="principalPhone">
              <el-input
                v-model="infoForm.principalPhone"
                maxlength="20"
                placeholder="请输入负责人电话"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="办公地址" prop="officeAddress">
              <el-input
                v-model="infoForm.officeAddress"
                maxlength="200"
                placeholder="请输入办公地址"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="icom-box">
            <el-form-item label="系统图标" prop="systemIcon">
              <el-upload
                class="avatar-uploader"
                action="#"
                :show-file-list="false"
                :limit="1"
                :multiple="false"
                :http-request="httpRequest"
              >
                <img
                  v-if="infoForm.systemIcon"
                  :src="infoForm.systemIcon"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属区县" prop="areaId">
              <el-radio-group
                v-model="infoForm.areaId"
                placeholder="请选择所属区县"
                style="line-height: 22px"
              >
                <el-radio v-for="v in areaOptions" :key="v.id" :label="v.id">{{
                  v.fullName
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyInfoList, getCompanyInfoById } from "@/api/baseData/company";
import { getSystemTypeList, uploader } from "@/api/common";
import { getSystemClassSelect } from "@/api/baseData/systemClass";
import { getAreaSelect } from "@/api/baseData/area";
import { addSystem, updataSystem, getSystemDetail } from "@/api/baseData/info";
import infoMixin from "@/mixins/info";
export default {
  name: "infoForm",
  mixins: [infoMixin],
  props: {
    type: {
      type: String,
      default: "add",
    },
    systemId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      visible: false,
      infoForm_loading: false,
      infoForm: {
        companyId: "", // 主体企业
        systemType: "", // 系统类型
        systemName: "", // 系统名称
        systemClass: "", // 系统分类
        systemRecordNumber: "",
        recordLicense: "",
        platform: "",
        domain: "",
        systemDownloadVolume: "",
        version: "",
        principalName: "",
        principalPhone: "",
        officeAddress: "",
        systemIcon: "",
        areaId: "",
      },
      infoRules: {
        systemName: {
          required: true,
          message: "请输入系统名称",
          trigger: "blur",
        },
        systemClass: {
          required: true,
          message: "请输入系统分类",
          trigger: "change",
        },
        systemType: {
          required: true,
          message: "请输入系统类型",
          trigger: "change",
        },
        companyId: {
          required: true,
          message: "请输入主体企业",
          trigger: "change",
        },
        areaId: {
          required: true,
          message: "请输入所属区县",
          trigger: "change",
        },
        pincipalPhone: {
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: "blur",
        },
      },
      value: "",
      companyOptions: [],
      listQuery_company: {
        pageIndex: 1,
        pageSize: 10,
      },
      total_company: 0,
      isShowCompanyInfo: false,
      companyInfo: {},
      systemTypeOptions: [],
      systemClassOptions: [],
      areaOptions: [],
    };
  },
  watch: {},
  mounted() {},
  created() {},
  methods: {
    // 请求公司列表
    async initCompanyList() {
      let { data } = await getCompanyInfoList(this.listQuery_company);
      this.companyOptions = [...this.companyOptions, ...data.list];
      this.total_company = data.pagination.total;
    },
    // 无限下拉
    nextCompanyList() {
      if (total_company == this.listQuery_company.length) return;
      this.listQuery_company.pageIndex++;
      this.initCompanyList();
    },
    async companyChange(val) {
      // 公司信息
      let { data } = await getCompanyInfoById(val);
      this.companyInfo = data;
      this.isShowCompanyInfo = true;
    },
   
    // 文件上传--------------------
    httpRequest(file) {
      uploader({
        type: 1,
        file: file.file,
      }).then((res) => {
        console.log(res, "上传成功");
      });
    },
    // 文件上传--------------------
   
    reset() {
      for (let key in this.infoForm) {
        this.infoForm[key] = undefined;
      }
      this.companyOptions = [];
      this.listQuery_company = {
        pageIndex: 1,
        pageSize: 10,
      };
      this.total_company = 0;
      this.companyInfo = {};
      this.isShowCompanyInfo = false;
    },
    close() {
      this.visible = false;
    },
    async openDialog() {
      this.visible = true;
      this.reset();
      this.infoForm_loading = true;
      await this.initCompanyList();
      await this.initSystemTypeList();
      await this.initAreaTypeList();
      this.type == "edit" && (await this.initForm());
      this.infoForm_loading = false;
    },
    async initForm() {
      let { data } = await getSystemDetail(this.systemId);
      this.infoForm = data;
      this.initSystemClassList(data.systemType);
      await this.companyChange(data.companyId);
      this.$forceUpdate();
    },
    async confirm() {
      this.$refs["infoForm"].validate((valid) => {
        if (valid) {
          switch (this.type) {
            case "add":
              addSystem(this.infoForm).then((res) => {
                this.$message({
                  message: "恭喜你，新增成功",
                  type: "success",
                });
                this.close();
              });
              break;
            case "edit":
              this.infoForm.id = this.initInfoForm.Id;
              updataSystem(this.infoForm).then((res) => {
                this.$message({
                  message: "恭喜你，修改成功",
                  type: "success",
                });
                this.close();
              });
              break;

            default:
              break;
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog-box {
  :deep(.el-dialog__body) {
    max-height: 60vh;
    overflow-y: scroll;
  }
}
.company-info {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  background: rgba(187, 187, 187, 0.23);
  margin-left: 80px;
  padding: 10px 20px;
  border-radius: 10px;
  width: 50%;
  margin-bottom: 10px;
  .company-info-items {
    width: 50%;
    div {
      line-height: 20px;
    }
  }
}
.icom-box {
  :deep(.avatar-uploader .el-upload) {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 100px;
      height: 100px;
      line-height: 100px;
      text-align: center;
    }
    .avatar {
      width: 100px;
      height: 100px;
      display: block;
    }
  }
}
</style>
@/api/baseData/area@/api/baseData/company