<template>
  <Base-dialog title="设置" width="700px" :isShow.sync="isShow" @submit="submit">
    <div class="day-set-box">
      <el-select v-model="atype" placeholder="请选择">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-form :model="{ list }" ref="ruleForm" label-width="400px" class="demo-ruleForm" label-position="left">
        <el-form-item :label="item.examName
        " v-for="(item, index) in list" :key="item.id" :prop="'list.' + index + '.examTime'"
          :rules="{ validator: validateDate, trigger: 'change' }">
          <el-date-picker style="witdh: 150px" v-model="item.examTime" type="date" placeholder="选择日期"
            :picker-options="pickerOptions" value-format="timestamp">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
  </Base-dialog>
</template>

<script>
import {
  addExamTime
} from "@/api/resource/good";
export default {
  data() {
    return {
      list: [],
      options: [
        {
          value: "1",
          label: "顺序添加",
        },
      ],
      atype: "1",
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 24 * 60 * 60 * 1000;
        },
      },
      isShow: false,
    };
  },
  methods: {
    init() {
      this.atype = "1";
      this.list = []
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },
    open(goodsId) {
      this.isShow = true;
      this.init();
      this.getList(goodsId)
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let list = JSON.parse(JSON.stringify(this.list));
          list.map(e => {
            if (e.examTime) {
              e.examTime /= 1000
            }
          })
          addExamTime(list).then(res => {
            this.$message.success("设置成功") ;
            this.isShow = false
          })
        } else {
          return false;
        }
      });
    },
    validateDate(rule, value, callback) {
      if (!value) {
        return callback();
      }
      let list = this.list.filter((e) => e.examTime == value);
      if (list.length > 1) {
        return callback(new Error("存在重复时间"));
      }
      return callback();
    },
    getList(goodsId) {
      this.$api.getBankGoodsExamList(goodsId).then((res) => {
        res.data.map(e => {
          if (e.examTime) {
            e.examTime *= 1000
          }
        })
        this.list = res.data;
      });
    },
  },

};
</script>

<style lang="scss" scoped>
.day-set-box {
  .demo-ruleForm {
    max-height: 500px;
    overflow-x: auto;
    margin-top: 30px;
    padding-left: 20px;

    /deep/ {
      .el-date-editor.el-input,
      .el-date-editor.el-input__inner {
        width: 150px;
      }
    }
  }
}
</style>