<template>
  <div>
    <el-dialog
      :title="dialogData.title"
      width="500px"
      :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"
      >
        <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>
        <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>

        <el-form-item label="有效期开始时间" prop="startTime">
          <el-date-picker
            v-model="addOrUpdateForm.startTime"
            type="datetime"
            placeholder="选择时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            :picker-options="startPickerOptions"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="有效期结束时间" prop="endTime">
          <el-date-picker
            v-model="addOrUpdateForm.endTime"
            type="datetime"
            placeholder="选择时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            :picker-options="endTimePickerOptions"
            style="width: 100%"
          />
        </el-form-item>
      </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
      },
      buyList: [],
      templateList: [],
      form: {
        templateId: null,
        correctList: {
          correct: [],
          incorrect: []
        }
      },
      // 规则
      rules: {
        account: [
          {
            // required: !!this.roleId,
            // 自定义验证函数，见上说明
            validator: (rule, value, callback) => {
              if (this.roleId === 2 && (!value || value === '')) {
                return callback('请选择所属账号');
              }
              return callback();
            },
            trigger: 'change'
          }
        ],
        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' }
        ],
        correctList: [
          {
            required: true,
            type: 'object',
            message: '模板文件不能为空!',
            trigger: ['change', 'blur'],
            validator: (rule, value, callback) => {
              if (
                Object.keys(value).length > 0 &&
                ((value.correct && value.correct.length > 0) ||
                  (value.incorrect && value.incorrect.length > 0))
              ) {
                return callback();
              }
              return callback('数据不能为空!');
            }
          }
        ]
      },
      dialogVisible: false,

      addOrUpdateForm: {
        account: '', // 所属账号
        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() {
      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) => {
        console.log(valid);
        if (!valid) {
          return false;
        }
        this.dialogVisible = false;
      });
    }
  }
};
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
.table {
  margin-top: 15px;
}
</style>
