<template>
  <div>
    <el-dialog
      :title="dialogData.title"
      width="900px"
      :visible.sync="dialogVisible"
      :destroy-on-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="formRef"
        :model="addOrUpdateForm"
        :rules="rules"
        label-position="left"
        label-width="120px"
      >
        <div class="wrap-content">
          <div class="wrap-content__item">
            <el-form-item v-if="roleId == 1" label="所属账号" prop="account" :required="roleId == 1">
              <el-select
                v-model="addOrUpdateForm.account"
                style="width: 100%"
                filterable
                clearable
                placeholder="请选择账号"
              >
                <el-option
                  v-for="item in accountList"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="任务名称" prop="taskName">
              <el-input
                v-model="addOrUpdateForm.taskName"
                placeholder="请输入任务名称"
              />
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="人员姓名" prop="name">
              <el-input
                v-model="addOrUpdateForm.name"
                placeholder="请输入人员姓名"
              />
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="身份证号" prop="IDNumber">
              <el-input
                v-model="addOrUpdateForm.IDNumber"
                placeholder="请输入身份证号"
              />
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="人员类型" prop="type">
              <el-select
                v-model="addOrUpdateForm.type"
                style="width: 100%"
                filterable
                clearable
                placeholder="请选择人员类型"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="有效期开始时间" prop="startTime">
              <el-date-picker
                v-model="addOrUpdateForm.startTime"
                type="datetime"
                placeholder="选择时间"
                value-format="yyyy-MM-dd"
                :picker-options="startPickerOptions"
                style="width: 100%"
              />
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="有效期结束时间" prop="endTime">
              <el-date-picker
                v-model="addOrUpdateForm.endTime"
                type="datetime"
                placeholder="选择时间"
                value-format="yyyy-MM-dd"
                :picker-options="endTimePickerOptions"
                style="width: 100%"
              />
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="点位">
              <el-input
                v-model="addOrUpdateForm.point"
                placeholder="请输入点位"
                clearable
              />
            </el-form-item>
          </div>
          <div class="wrap-content__item">
            <el-form-item label="商铺名称">
              <el-input
                v-model="addOrUpdateForm.tenantName"
                placeholder="请输入商铺名称"
                clearable
              />
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cloneFn">取 消</el-button>
        <el-button
          type="primary"
          :loading="dialogData.submitLoading"
          @click="submit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { isID } from '@/utils/validate.js';
export default {
  components: { },
  props: {
    typeList: {
      type: Array,
      default: () => []
    },
    accountList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogData: {
        title: '添加人员',
        submitLoading: false
      },
      // 规则
      rules: {
        taskName: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入人员姓名', trigger: 'blur' }
        ],
        IDNumber: [
          { required: true, message: '请输入身份证号码', trigger: 'blur' },
          {
            required: true,
            // 自定义验证函数，见上说明
            validator: (rule, value, callback) => {
              if (!isID(value).state) {
                return callback(isID(value).data);
              }
              return callback();
            },
            trigger: 'blur'
          }
        ],
        startTime: [
          { required: true, message: '请选择有效期开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择有效期结束时间', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择人员类型', trigger: 'change' }
        ]
      },
      dialogVisible: false,

      addOrUpdateForm: {
        taskName: '', // 任务名称
        name: '', // 姓名
        IDNumber: '', // 身份证号
        type: '', // 人员类型
        point: '', // 点位
        tenantName: '', // 商铺名称
        startTime: null,
        endTime: null
      }
    };
  },
  computed: {
    roleId() {
      return this.$store.getters.role;
    },
    // 批量注册-返回向下取整，减少渲染次数
    makePercent() {
      return Math.floor((this.hasDealNumber / this.allNumber) * 100);
    },
    startPickerOptions() {
      const _this = this;
      return {
        disabledDate(time) {
          if (_this.addOrUpdateForm.endTime && _this.addOrUpdateForm.endTime !== '') {
            return time.getTime() > new Date(_this.addOrUpdateForm.endTime).getTime(); // 禁用true值
          }
          return false;
        }
      };
    },
    endTimePickerOptions() {
      const _this = this;
      return {
        disabledDate(time) {
          if (_this.addOrUpdateForm.startTime && _this.addOrUpdateForm.startTime !== '') {
            return time.getTime() < new Date(_this.addOrUpdateForm.startTime).getTime(); // 禁用true值
          }
          return false;
        }
      };
    }
  },
  watch: {},
  created() {
  },
  methods: {
    open(data) {
      console.log(data);
      this.dialogData.title = data?.isUpdate ? '编辑人员' : '添加人员';
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.formRef.resetFields();
      });
    },
    // 取消
    cloneFn() {
      this.dialogVisible = false;
      this.$refs.formRef?.clearValidate(); // 清空表单校验
      this.addOrUpdateForm = {
        entryName: '', // 入口名称
        num: 1, // 序号
        openTime: null, // 开放时间
        timePeriod: [
          {
            key: new Date().getTime() + 1,
            num: 0,
            timeRange: null
          }
        ]
      };
    },
    submit(e) {
      e.preventDefault();
      this.$refs.formRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.dialogVisible = false;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
.table {
  margin-top: 15px;
}
.wrap-content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  &__item {
    width: calc(50% - 10px);
  }
}
</style>
