<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId" label="部门">
          <!--          <a-input placeholder="请输入部门编号" v-model="model.departId"/>-->
          <!--          <j-select-depart placeholder="请选择部门" v-model="model.departId"/>-->
          <j-dict-select-tag type="list" v-model="model.departId" dictCode="sys_depart,depart_name,id,org_category='2'"
                             @change="getEnterprisesManagerValChange()"
                             placeholder="请选择部门"/>

        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupName" label="班组名称">
          <a-input placeholder="请输入班组名称" v-model="model.groupName"/>
        </a-form-model-item>


        <!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enterprisesManager" label="负责人">-->
        <!--          <j-select-user-by-dep placeholder="请选择负责人" v-model="model.enterprisesManager" :multi="false"-->
        <!--                                @change="getMobile"></j-select-user-by-dep>-->
        <!--          {{ model.enterprisesManager }}-->
        <!--        </a-form-model-item>-->


        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" prop="enterprisesManager" label="负责人">
          <!--          <a-select :allowClear="true"-->
          <!--                    style="width: 100%" v-model="model.enterprisesManager">-->
          <!--            <a-select-option v-for="record in this.model.records" :key="record.id " :disabled="model.departId==null">-->
          <!--              {{ record.username }}-->
          <!--            </a-select-option>-->
          <!--          </a-select>-->
          <j-dict-select-tag type="list" v-model="model.enterprisesManager"
                             placeholder="请选择负责人" :dict-code="enterprisesManager" @change="getMobile()"
                             :disabled="model.departId==null"/>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile" label="手机号">
          <a-input placeholder="请输入手机号" v-model="model.mobile"/>
          <!--          <j-dict-select-tag type="list" v-model="model.mobile" :dictCode="mobileVal"></j-dict-select-tag>-->
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import {httpAction, getAction} from '@/api/manage'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'

export default {
  name: "GroupxModal",
  components: {
    JDictSelectTag,
    JSelectUserByDep,
  },
  data() {
    return {
      enterprisesManager: "sys_user,realname,id",
      title: "操作",
      visible: false,
      // mobileVal: "",
      model: {
        // records: [],
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },

      confirmLoading: false,
      validatorRules: {
        departId: [
          {required: true, message: "部门不能为空"},
        ],
        groupName: [
          {required: true, message: '班组名称不能为空!'},
          {pattern: /(^[\u4e00-\u9fa5_a-zA-Z0-9_]{1,10}$)/, message: '班组名称不能为空且长度不能超过10！'}
        ],
        enterprisesManager: [
          {required: true, message: "负责人不能为空"},
        ],
        // mobile: [
        //   {required: false},
        //   {pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
        // ],

      },
      url: {
        add: "/groupx/add",
        edit: "/groupx/edit",
        sysUserList: "/sys/user/list",
      },
      // 选择用户查询条件配置
      selectUserQueryConfig: [
        {key: 'phone', label: '电话'},
      ],
    }
  },
  created() {
  },
  methods: {

    getEnterprisesManagerValChange() {
      this.enterprisesManager = "sys_user,realname,id,id in (select user_id from sys_user_depart where dep_id='" + this.model.departId + "')";
      // this.enterprisesManager = "zy_process,process_name,id,id in(select process_id from zy_product_process where product_id='1')";
    },

    getMobile() {
      let that = this;
      let param = {
        "id": that.model.enterprisesManager,
        // "departId": that.model.departId,
      }
      // alert(param.toString());
      getAction(that.url.sysUserList, param).then((res) => {
        if (res.success) {
          // console.log("----------" + JSON.stringify(res.result.records))
          let phone = JSON.stringify(res.result.records[0].phone);
          if (phone.length > 11) {
            phone = phone.substring(1, 12);
          } else {
            phone = "";
          }
          that.model = Object.assign(that.model, {'mobile': phone});
          this.$forceUpdate();
        }
      });
    },
    add() {
      //初始化默认值
      this.edit({});
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    close() {
      this.$emit('close');
      this.visible = false;
      this.$refs.form.clearValidate();
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        } else {
          return false;
        }
      })
    },
    handleCancel() {
      this.close()
    },


  }
}
</script>

<style lang="less" scoped>

</style>