<template>
  <div class="ct">
    <el-dialog  :visible.sync="counterDialog" width="800px"
      class="dialog" :show-close="false" :before-close="handleClose" top="5vh"
    >
      <div class="choose">
        <div class="title">
          <div class="cg">添加柜组</div>
          <div class="circle" @click="handleClose">
            <d2-icon-svg name="close" class="icon" />
          </div>
        </div>
        <div class="br">
          <el-form
            class="searchzone"
            :model="formData"
            label-width="auto"
            ref="addCounter"
            :rules="rules"
          >
            <el-form-item label="柜组名称:" prop="name">
              <el-input
                size="small"
                v-model="formData.name"
                style="width:240px"
                placeholder="请输入柜组名称"
              />
            </el-form-item>
            <el-form-item label="柜组负责人:" prop="principal">
              <el-select
                size="small"
                v-model="formData.principal"
                placeholder="请选择柜组负责人"
                style="width:240px"
              >
                <el-option
                  v-for="item in personList"
                  :key="item.id"
                  :label="item.userName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="柜组编号:" prop="num">
              <el-input
                size="small"
                v-model="formData.num"
                style="width:240px"
                placeholder="请输入柜组编号"
              />
            </el-form-item>
            <el-form-item label="所在区域:" prop="area">
              <el-input
                size="small"
                v-model="formData.area"
                style="width:240px"
                placeholder="请输入所在区域"
              />
            </el-form-item>
            <div class="cs">
              <el-form-item label="门店:" prop="stallIds">
                <el-transfer
                  style="text-align: left; display: inline-block"
                  v-model="formData.stallIds"
                  :left-default-checked="[]"
                  :right-default-checked="[]"
                  :titles="['选择', '已选']"
                  :button-texts="['删除', '添加']"
                  @change="handleChange"
                  :data="stallData"
                >
                  <span slot-scope="{ option }">{{ option.name }}</span
                  >
                </el-transfer>
              </el-form-item>
            </div>
          </el-form>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel('addCounter')" size="small"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="handleFinish('addCounter')"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { stallDetail, stallSave } from "@/api/in/counter"
export default {
  data() {
    const nameValidate = (rule, value, callback) => {
      let reg = /^[a-zA-Z\u4e00-\u9fa5]+$/
      if (!reg.test(value)) {
          callback(new Error('含有非法字符(只能输入字母、汉字)!'))
        } else {
          callback()
        }
  }
    return {
      counterDialog: false,
      value4: [],
      formData: {
        name: "",
        num: "",
        principal:"",
        area:'',
        stallIds: [],
      },
      personList: [],
      stallData: [],
      rules: {
        name: [{ required: true, message: "请输入柜组名称", trigger: "blur" },
              {  max: 50, message: '长度在50个字符以内', trigger: 'blur' },
              { validator: nameValidate, trigger: "blur" }],

        principal: [
          { required: true, message: "请选择活动负责人", trigger: "change" }
        ],
        num: [{ required: true, message: "请输入柜组编号", trigger: "blur" },
              {  max: 50, message: '长度在50个字符以内', trigger: 'blur' }],
        stallIds:[
          { required: true, message: "请选择门店", trigger: "blur" },
          { required: true, message: "请选择门店", trigger: "change" },
        ],
        area: [{ required: true, message: "请输入所在区域", trigger: "blur" },
               {  max: 50, message: '长度在50个字符以内', trigger: 'blur' },
               { validator: nameValidate, trigger: "blur" }]

      }
    };
  },
  mounted(){
    this.getPersonList()
  },
  methods: {
    handleChange() {},
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.counterDialog = false;
        })
        .catch(_ => {});
    },
    handleCancel(formName) {
      this.$refs[formName].resetFields();
      this.counterDialog = false;
    },
    handleFinish(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 调用新增接口
          // this.counterDialog = false;
          let formData = JSON.parse(JSON.stringify(_this.formData))
          delete  formData.stallIds
          formData.stallIds = _this.formData.stallIds.join(',')
          stallSave(formData).then(res=>{
            _this.$message(formData.res.data)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 柜组负责人 门店列表
    getPersonList(){
      let _this = this
      stallDetail().then(res => {
        _this.personList = res.data.principals_unselected
        _this.stallData = res.data.stalls_unselected.map((v,i)=>{
          v.key = v.id
          return v
        })

      })
    }
  },
   watch:{
      counterDialog(){
        if(this.counterDialog){
          if(this.$refs.addCounter){
            this.$refs.addCounter.resetFields();
          }
        }
      }
    }
};
</script>

<style scoped>
.dialog >>> .el-dialog {
  height: calc(100vh - 208px);
  overflow: auto;
}
.cs {
  display: flex;
}
.ct >>> .el-transfer-panel {
  width: 180px;
}
.choose {
  padding: 16px;
  font-size: 16px;
  font-weight: bold;
  color: rgba(56, 56, 56, 1);
  box-sizing: border-box;
}
.title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 10px;
}
.transfer-footer {
  margin-left: 20px;
  padding: 6px 5px;
}
.ct >>> .el-dialog__header {
  padding: 0 !important;
}
.ct >>> .el-dialog__body {
  padding: 0;
}
.circle {
  width: 30px;
  height: 30px;
  line-height: 30px;
  border-radius: 50%;
  border: 1px solid rgba(208, 2, 27, 1);
  position: relative;
}
.circle >>> .icon {
  width: 28px;
  height: 28px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.br {
  border-top: 1px solid #f8f8f8;
  border-bottom: 1px solid #f8f8f8;
  padding: 24px 60px;
}
</style>
