<template>
  <el-dialog
    title="协会管理"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="600px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="95px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="协会名称：" prop="assocName">
            <el-row :gutter="5">
              <el-col :span="23">
                <el-input
                  type="input"
                  :autosize="{ minRows: 2}"
                  placeholder="请输入协会名称"
                  v-model="baseForm.assocName">
                </el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="管辖范围：" prop="areaIds">
            <el-row :gutter="5">
              <el-col :span="23">
                <select-tree
                  style="width: 100%"
                  :treeData="treeData"
                  :placeholder="'请输入产品名称/批次号'"
                  @handlerClick="handlerClick"
                  @getCheckTree="getCheckTreeHandler"
                  @getCheckTreeNode="getCheckTreeNodeHandler"
                  :checkVal="cusareaName"
                  :checkedList="areaIds"
                  checkbox
                  v-model="baseForm.areaIds"
                ></select-tree>
                <div class="areatText" type="">
                  <el-tag
                    v-for="item in treeNodeList"
                    :key="item.cusareaId"
                  >
                    {{ item.cusareaName || item}}
                  </el-tag>
                </div>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="负责人：" prop="assocManageid">
            <el-row :gutter="5">
              <el-col :span="23">
                <el-select
                  v-model="baseForm.assocManageid"
                  filterable
                  style="width: 100%"
                  placeholder="请选择负责人"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.id"
                    :label="item.userName"
                    :value="item.userId"
                  >
                  </el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="协会介绍：" prop="assocRemark">
            <el-row :gutter="5">
              <el-col :span="23">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 4}"
                  placeholder="请输入协会介绍"
                  maxlength="500"
                  show-word-limit
                  v-model="baseForm.assocRemark" />
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div style="text-align: right">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { addAssoc, editAssoc } from '@/api/assoc/index.js'
import { assign } from 'lodash-es';
import SelectTree from '@/components/SelectTree';

export default {
  name: 'associationManage',
  components: {
    SelectTree
  },
  props: {
    data: {
      default() {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default: false
    },
    userList: {
      type: Array,
      default: () => []
    },
    treeData: {
      type: Array,
      default: () => []
    },
    typeAssoc: {
      type: String
    },
    editRow: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      cusareaName:'',
      dialogSubmitLoading: false,
      dialogVisible: false,
      treeNodeList: [],
      baseForm: {
        assocName: '',
        assocManageid: '',
        assocRemark: '',
        areaIds: []
      },
      areaIds: '',
      syinfoList: [],
      hasFetched: false,
      rules: {
        assocManageid: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ],
        assocName: [
          { required: true, message: '请输入您的协会名称', trigger: 'blur' }
        ],
        assocRemark: [
          { required: true, message: '请输入您的协会介绍', trigger: 'blur' }
        ],
        areaIds: [
          { required: true, message: '请输入您的管辖范围', trigger: 'blur' }
        ],
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
          if (!this.hasFetched) {
            this.hasFetched = true;
          }
        }
      }
    },
    data: {
      deep: true,
      handler(val) {
        assign(this.baseForm, val);
      }
    },
    editRow: {
      handler(val) {
        this.baseForm.assocName = val.assocName
        this.baseForm.assocRemark = val.assocRemark
        this.baseForm.assocManageid = val.assocManageid
        this.baseForm.areaIds = val.areaIds
        this.areaIds = val.areaIds
        const treeNodeList = val?.cusareaNames?.split(',') || []
        this.cusareaName = treeNodeList[treeNodeList.length - 1]
        this.treeNodeList = treeNodeList
      },
      deep: true
    }
  },
  methods: {
    getCheckTreeHandler(val) {
      this.baseForm.areaIds = val.join(',')
    },
    getCheckTreeNodeHandler(val) { 
      this.treeNodeList = val
    },
    handlerClick() { },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    resetFormData() {
      this.baseForm = {
        assocName: '',
        assocManageid: '',
        assocRemark: '',
        areaIds: []
      };
    },
    async addAssocHandler() {
      if (this.editAssoc === 'add') {
        const params = {
          ...this.baseForm
        }
        await addAssoc(params)
      } else { 
        const params = {
          ...this.baseForm,
          assocId: this.editRow.assocId
        }
        await editAssoc(params)
      }
      this.handleClose()
      this.$emit('getList')
      this.resetFormData()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        valid && this.addAssocHandler()
      })
    },
    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-tag{
  margin-right: 5px;
}
.areatText{
  width: 100%;
  height: 100px;
  border:1px solid #DCDFE6;
  margin-top: 10px;
  padding:2% 3%;
}
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
