<template>
  <div id="issueAcertificate">
    <div class="dis_play_sty">
      <el-form-item label="请选择" prop="certificateTypeId">
        <el-select
          v-model="listData.certificateTypeId"
          placeholder="请选择证书类型"
          @change="editStatus = true"
          clearable
        >
          <el-option
            v-for="(item, index) in certificate"
            :key="index"
            :label="item.certificateName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="certificateList.length"
        label=" "
        label-width="10px"
        prop="certificateId"
      >
        <el-select
          v-model="listData.certificateId"
          placeholder="请选择证书名称"
          @change="editStatus = true"
          clearable
        >
          <el-option
            v-for="(item, index) in certificateList"
            :key="index"
            :label="item.certificateName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="certificateTpList.length"
        label=" "
        label-width="10px"
        prop="certificateTpId"
      >
        <el-select
          v-model="listData.certificateTpId"
          placeholder="请选择证书模板"
          clearable
        >
          <el-option
            v-for="(item, index) in certificateTpList"
            :key="index"
            :label="item.title"
            :value="item.tpId"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  props: ["listData"],
  data() {
    return {
      certificateList: [],
      certificateTpList: [],
      editStatus: false, //避免第一次进来初始化数据丢失
    };
  },
  watch: {
    "listData.certificateTypeId": {
      handler(val, oldVal) {
        if (this.editStatus) {
          this.$set(this.listData, "certificateId", "");
        }
        if (val) {
          this.$api
            .inquireBaseCertificatecommon({ status: 1, certificateTypeId: val })
            .then((res) => {
              this.certificateList = res.rows;
            });
        } else {
          this.certificateList = [];
        }
      },
      immediate: true,
    },
    "listData.certificateId": {
      handler(val, oldVal) {
        if (this.editStatus) {
          this.$set(this.listData, "certificateTpId", "");
        }
        if (val) {
          this.$api
            .inquireBaseCertificatetp({ status: 1, certificateId: val })
            .then((res) => {
              this.certificateTpList = res.rows;
            });
        } else {
          this.certificateTpList = [];
        }
      },
      immediate: true,
    },
  },
  computed: { ...mapGetters(["certificate"]) },
  mounted() {
    this.$api.inquireBaseCertificatetp({ status: 1 }).then((res) => {
      this.certificateOption = res.rows;
    });
  },
  methods: {},
};
</script>

<style lang="less" scoped>
.dis_play_sty {
  display: flex;
  align-items: center;
}
</style>
