<template>
  <a-modal
    centered
    class="businessOwnerSettings-add-modal"
    :footer="null"
    :maskClosable="false"
    :title="itemId ?'编辑人员':'新增人员'"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        ref="ruleForm"
        :model="form"
        :label-col="labelCol"
        id="businessOwnerSettings-add-form"
        :wrapper-col="wrapperCol"
        :rules="rules"
      >
        <a-form-model-item label="人员名称" prop="userSn">
          <userName ref="userName" id="businessOwnerSettings-add-userSn" @change="userChange"></userName>
        </a-form-model-item>
        <a-form-model-item label="人员类型" prop="bizUserType">
          <v-select
            v-model="form.bizUserType"
            ref="printStatus"
            id="businessOwnerSettings-add-printStatus"
            code="BIZ_USER_TYPE"
            placeholder="请选择人员类型"
            allowClear></v-select>
        </a-form-model-item>
        <a-form-model-item label="管辖经销商" prop="allDealerFlag">
          <a-radio-group v-model="form.allDealerFlag" id="businessOwnerSettings-add-allDealerFlag">
            <a-radio value="1">
              全部经销商
            </a-radio>
            <a-radio value="0">
              部分经销商
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="管辖品类" prop="allProductFlag">
          <a-radio-group v-model="form.allProductFlag" id="businessOwnerSettings-add-allProductFlag">
            <a-radio value="1">
              全部品类
            </a-radio>
            <a-radio value="0">
              部分品类
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 供应商 -->
        <a-form-model-item label="供应商" prop="allSupplierFlag" v-if="form.bizUserType == 'cg'">
          <a-radio-group v-model="form.allSupplierFlag" id="businessOwnerSettings-add-allSupplierFlag">
            <a-radio value="1">
              全部供应商
            </a-radio>
            <a-radio value="0">
              部分供应商
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 人员类型为仓库管理员时 显示管辖仓库 -->
        <a-form-model-item label="管辖仓库" prop="bizUserScopeList" v-if="form.bizUserType == 'cg'">
          <chooseWarehouse ref="warehouse" id="businessOwnerSettings-add-bizUserType" modeType="multiple" :value="warehouseList" @change="handleWarehouse"></chooseWarehouse>
        </a-form-model-item>
        <div style="display: flex;justify-content: center;padding: 30px 0;">
          <a-button id="businessOwnerSettings-close-btn" @click="isShow=false">取消</a-button>
          <a-button id="businessOwnerSettings-save-btn" style="margin-left: 15px;" type="primary" @click="handleSave">{{ itemId?'保存':'确定' }}</a-button>
        </div>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
// 组件
import userName from '@/views/common/userName'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import { VSelect } from '@/components'
// 接口
import { bizuserSave } from '@/api/bizuser.js'
export default {
  name: 'BusinessOwnerSettings',
  components: { VSelect, userName, chooseWarehouse },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      form: {
        userSn: '', // 人员名称
        bizUserType: undefined, // 人员类型
        allDealerFlag: undefined, // 管辖经销商
        allProductFlag: undefined, // 管辖产品
        bizUserScopeList: [], // 管辖仓库
        allSupplierFlag: undefined// 供应商
      },
      warehouseList: [], // 管辖仓库
      user_name: '', // 用户名称
      // 表单label布局
      labelCol: { span: 5 },
      wrapperCol: { span: 16 },
      // 验证表单规则
      rules: {
        userSn: [{ required: true, message: '请输入人员名称', trigger: 'change' }],
        bizUserType: [{ required: true, message: '请选择人员类型', trigger: 'change' }],
        allDealerFlag: [{ required: true, message: '请选择管辖经销商', trigger: 'change' }],
        allProductFlag: [{ required: true, message: '请选择管辖产品', trigger: 'change' }],
        bizUserScopeList: [{ required: true, message: '请选择管辖仓库', trigger: 'change' }],
        allSupplierFlag: [{ required: true, message: '请选择供应商', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.spinning = true
          _this.form.id = _this.itemId ? _this.itemId : ''
          if (_this.form.bizUserType != 'cg') {
            _this.warehouseList = []
            _this.form.bizUserScopeList = []
            _this.form.allSupplierFlag = undefined
          }
          bizuserSave(_this.form).then(res => {
            _this.spinning = false
            if (res.status == 200) {
              _this.$emit('ok', res.data)
              _this.isShow = false
            }
          }).catch(err => {
            _this.spinning = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 管辖仓库 change
    handleWarehouse (val) {
      this.warehouseList = val
      const arr = []
      val.forEach(item => {
        const obj = {
          bizSn: item
        }
        arr.push(obj)
      })
      this.form.bizUserScopeList = arr
    },
    // 重置
    resetSearchForm () {
      this.user_name = ''
      if (this.form.bizUserType && this.form.bizUserType == 'cg') {
        this.$refs.warehouse.clearData()

        this.form.bizUserScopeList = []
      }
      this.$refs.userName.resetForm()
      this.$refs.ruleForm.resetFields()
    },
    // 人员名称
    userChange (val) {
      this.form.userSn = val.key
    },
    // 获取编辑回显数据
    getEditInfo (obj) {
      this.$refs.userName.setDufaultVal(obj.userName)
      const arr = []
      const newarr = []
      if (obj.warehouseList && obj.warehouseList.length > 0) {
        obj.warehouseList.forEach(item => {
          arr.push(item.warehouseSn)
          const newObj = {
            bizSn: item.warehouseSn
          }
          newarr.push(newObj)
        })
        this.warehouseList = arr
      }
      this.form = {
        userSn: obj.userSn,
        bizUserType: obj.bizUserType, // 人员类型
        allDealerFlag: obj.allDealerFlag,
        allProductFlag: obj.allProductFlag,
        bizUserScopeList: newarr,
        allSupplierFlag: obj.allSupplierFlag
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.resetSearchForm()
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
  .inventoryCheckAddModal-modal{
    .ant-modal-body {
      padding: 30px 40px 24px;
    }
    .inventoryCheckAddModal-con{
      text-align: center;
      h3{
        font-size: 16px;
        font-weight: bold;
        margin-bottom: 25px;
      }
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
