<template>
  <el-dialog
    title="IC卡信息"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="660px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="95px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="用户名：" prop="farmerId">
            <el-row :gutter="5">
              <el-col :span="23">
                <el-select
                  placeholder="请输入用户名"
                  filterable
                  style="width: 100%"
                  v-model="baseForm.farmerId">
                  <el-option
                    v-for="item in userList"
                    :key="item.farmerId"
                    :label="item.farmerName + '-' + item.farmerPhone"
                    :value="item.farmerId">
                  </el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="IC卡号：" prop="cardNo">
            <el-row :gutter="5">
              <el-col :span="23">
                <el-input
                  v-model="baseForm.cardNo"
                  style="width: 100%"
                  placeholder="请输入IC卡号"
                />
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div style="text-align: right">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { addCard } from '@/api/card/index.js'
import { assign } from 'lodash-es';
export default {
  name: 'addedICCard',
  props: {
    data: {
      default() {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default: false
    },
    areaId: {
      type: String,
      default: ''
    },
    userList:{
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogSubmitLoading: false,
      dialogVisible: false,
      baseForm: {
        farmerName: '',
        farmerId:'',
        cardNo:''
      },
      syinfoList: [],
      hasFetched: false,
      rules: {
        cardNo: [
          { required: true, message: '请输入IC卡', trigger: 'blur' }
        ],
        farmerId: [
          { required: true, message: '请选择用户名', trigger: 'blur' }
        ],
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
          if (!this.hasFetched) {
            this.hasFetched = true;
          }
        }
      }
    },
    data: {
      deep: true,
      handler(val) {
        assign(this.baseForm, val);
      }
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.resetFormData();
      this.dialogVisible = false;
    },
    resetFormData() {
      this.baseForm = {};
    },
    async handlerAddCard() {
      const res = await addCard(this.baseForm)
      if(res?.code === "000000") {
        this.handleClose()
        this.$emit('refresh')
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        valid && this.handlerAddCard()
      })
    },

    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
