<template>
  <a-modal
    centered
    class="businessOwnerSettings-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"
        :wrapper-col="wrapperCol"
        :rules="rules"
      >
        <a-form-model-item label="人员名称" prop="userSn">
          <userName ref="userName" id="businessOwnerSettings-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-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">
            <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">
            <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">
            <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" modeType="multiple" :value="warehouseList" @change="handleWarehouse"></chooseWarehouse>
        </a-form-model-item>
        <div style="display: flex;justify-content: center;padding: 30px 0;">
          <a-button type="primary" @click="handleSave">{{ itemId?'保存':'确定' }}</a-button>
          <a-button style="margin-left: 15px;" @click="isShow=false">取消</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: 'ProductInfoDetailModal',
  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: '',
      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
        }
      })
    },
    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>
