<template>
  <a-modal
    centered
    class="bindEdit-modal"
    :footer="null"
    :maskClosable="false"
    title="账号授权"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div>
        <a-form-model
          id="bindEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="选择中心店" prop="ownerOrgSn">
            <a-select
              placeholder="请选择中心店"
              id="productInfoList-ownerOrgSn"
              allowClear
              v-model="form.ownerOrgSn"
              :disabled="itemSn?true:false"
              :showSearch="true"
              option-filter-prop="children"
              :filter-option="filterOption"
              @change="orgChange">
              <a-select-option v-for="item in orgList" :key="item.orgSn" :value="item.orgSn">{{ item.orgName }}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="选择授权账号" prop="userSn">
            <a-select
              placeholder="请选择授权账号"
              id="productInfoList-userSn"
              allowClear
              v-model="form.userSn"
              :disabled="itemSn?true:false"
              :showSearch="true"
              option-filter-prop="children"
              :filter-option="filterOption">
              <a-select-option v-for="item in userList" :key="item.sn" :value="item.sn" :disabled="item.status=='0'">{{ item.name }} - {{ item.loginName }}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="选择配送店" prop="orgSnList">
            <a-select
              placeholder="请选择配送店"
              id="productInfoList-orgSnList"
              allowClear
              v-model="form.orgSnList"
              mode="multiple"
              :showSearch="true"
              option-filter-prop="children"
              :filter-option="filterOption">
              <a-select-option v-for="item in childList" :key="item.sn" :value="item.sn">{{ item.name }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="bindEdit-modal-save" @click="handleSave">保存</a-button>
          <a-button id="bindEdit-modal-back" @click="isShow = false" style="margin-left: 15px;">关闭</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { jxsorgRelationDetail, jxsorgRelationQueryList } from '@/api/jxsorg'
import { userOrgUserList, userOrgDetail, userOrgSave } from '@/api/userOrg'
export default {
  name: 'UserAuthEditModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        ownerOrgSn: undefined,
        userSn: undefined,
        orgSnList: undefined
      },
      rules: {
        ownerOrgSn: [ { required: true, message: '请选择中心店', trigger: 'change' } ],
        userSn: [ { required: true, message: '请选择授权账号', trigger: 'change' } ],
        orgSnList: [ { required: true, message: '请选择配送店', trigger: 'change' } ]
      },
      orgList: [],
      userList: [],
      childList: []
    }
  },
  methods: {
    // 中心店 change
    orgChange (val) {
      this.getUserList()
      this.getChildList()
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    //  详情
    getDetail () {
      const _this = this
      userOrgDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          _this.form.ownerOrgSn = res.data.ownerOrgSn
          _this.getUserList()
          _this.getChildList()
          _this.form.userSn = res.data.userSn
          _this.form.orgSnList = []
          if (res.data.orgList) {
            res.data.orgList.map(item => {
              _this.form.orgSnList.push(item.sn)
            })
          }
        }
      })
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.spinning = true
          userOrgSave(_this.form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              setTimeout(function () {
                _this.isShow = false
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 中心店数据
    getOrgList () {
      jxsorgRelationQueryList({}).then(res => {
        if (res.status == 200) {
          this.orgList = res.data
        } else {
          this.orgList = []
        }
      })
    },
    // 授权账号数据
    getUserList () {
      userOrgUserList({ orgSn: this.form.ownerOrgSn }).then(res => {
        if (res.status == 200) {
          if (res.status == 200) {
            this.userList = res.data
          } else {
            this.userList = []
          }
        }
      })
    },
    // 配送店数据
    getChildList () {
      jxsorgRelationDetail({ sn: this.form.ownerOrgSn }).then(res => {
        if (res.status == 200) {
          this.childList = res.data.childList || []
        } else {
          this.childList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.form.orgSnList = []
        this.orgList = []
        this.userList = []
        this.childList = []
      } else {
        this.getOrgList()
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .bindEdit-modal{
    .ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .btn-cont {
    	text-align: center;
    	margin: 35px 0 10px;
    }
  }
</style>
