<template>
  <div class="page-wrapper role-add">
    <div class="page-container">
      <a-collapse
        v-model="activeName"
        expand-icon-position="right"
        class="collapse-header-divide"
      >
        <a-collapse-panel key="a" :header="$t('CONFIG_INFO')">
          <a-table
            ref="table"
            size="middle"
            :columns="columns"
            :data-source="loadData"
            :pagination="false"
            :alert="true"
            :loading="tableLoading"
            :row-key="(record) => record.id"
          >
            <template slot="columnComment" slot-scope="text, record">
              <a-input v-model="record.columnComment" />
            </template>
            <template slot="javaType" slot-scope="text, record">
              <a-select
                v-model="record.javaType"
                style="width: 120px"
                :disabled="judgeColumns(record)"
              >
                <a-select-option
                  v-for="(item, index) in javaTypeData"
                  :key="index"
                  :value="item.value"
                  >{{ item.label }}</a-select-option
                >
              </a-select>
            </template>
            <template slot="effectType" slot-scope="text, record">
              <a-select
                v-model="record.effectType"
                style="width: 120px"
                :disabled="judgeColumns(record)"
              >
                <a-select-option
                  v-for="(item, index) in effectTypeData"
                  :key="index"
                  :value="item.value"
                  >{{ item.label }}</a-select-option
                >
              </a-select>
            </template>
            <template slot="dictTypeCode" slot-scope="text, record">
              <a-select
                v-model="record.dictTypeCode"
                style="width: 120px"
                :disabled="
                  record.effectType !== 'radio' &&
                  record.effectType !== 'select' &&
                  record.effectType !== 'checkbox'
                "
              >
                <a-select-option
                  v-for="(item, index) in dictDataAll"
                  :key="index"
                  :value="item.dictCode"
                  >{{ item.dictName }}</a-select-option
                >
              </a-select>
            </template>
            <template slot="whetherTable" slot-scope="text, record">
              <a-checkbox v-model="record.whetherTable" />
            </template>
            <template slot="whetherRetract" slot-scope="text, record">
              <a-checkbox v-model="record.whetherRetract" />
            </template>
            <template slot="whetherAddUpdate" slot-scope="text, record">
              <a-checkbox
                v-model="record.whetherAddUpdate"
                :disabled="judgeColumns(record)"
              />
            </template>
            <template slot="whetherCode" slot-scope="text, record">
              <a-checkbox
                v-model="record.whetherCode"
                :disabled="judgeColumns(record)"
              />
            </template>
            <template slot="whetherRequired" slot-scope="text, record">
              <a-checkbox
                v-model="record.whetherRequired"
                :disabled="judgeColumns(record)"
              />
            </template>
            <template slot="queryWhether" slot-scope="text, record">
              <a-switch v-model="record.queryWhether">
                <a-icon
                  slot="checkedChildren"
                  class="ami-core-icon"
                  type="check"
                />
                <a-icon
                  slot="unCheckedChildren"
                  class="ami-core-icon"
                  type="close"
                />
              </a-switch>
            </template>
            <template slot="queryType" slot-scope="text, record">
              <a-select
                v-model="record.queryType"
                style="width: 100px"
                :disabled="!record.queryWhether"
              >
                <a-select-option
                  v-for="(item, index) in codeGenQueryTypeData"
                  :key="index"
                  :value="item.value"
                  >{{ item.label }}</a-select-option
                >
              </a-select>
            </template>
          </a-table>
        </a-collapse-panel>
      </a-collapse>
    </div>
    <div class="page-bottom-op">
      <Button type="primary" @click="handleConfirm()">
        <Icon type="icon-cur-save_1" :size="18" />{{ $t("SAVE") }}
      </Button>
      <Button @click="handleCancel">
        <Icon type="icon-dir-f-rollback" :size="18" />{{ $t("BACK") }}
      </Button>
    </div>
  </div>
</template>

<script>
import { Icon } from "@mega-design/ui-vue";
import ApiConstant from "@/api/ApiConstant";

export default {
  components: { Icon },
  data() {
    return {
      activeName: "a",
      // 表头
      columns: [
        {
          title: `java${this.$t("FILED")}`,
          dataIndex: "javaName",
        },
        {
          title: this.$t("CORE_FILED_DESC"),
          dataIndex: "columnComment",
          scopedSlots: { customRender: "columnComment" },
        },
        {
          title: this.$t("CORE_PHYSICAL_TYPE"),
          dataIndex: "dataType",
        },
        {
          title: `java${this.$t("TYPE")}`,
          dataIndex: "javaType",
          scopedSlots: { customRender: "javaType" },
        },
        {
          title: this.$t("ACTION") + this.$t("TYPE"),
          dataIndex: "effectType",
          scopedSlots: { customRender: "effectType" },
        },
        {
          title: this.$t("DICTIONARY"),
          dataIndex: "dictTypeCode",
          scopedSlots: { customRender: "dictTypeCode" },
        },
        {
          title: this.$t("LIST_DISPLAY"),
          align: "center",
          dataIndex: "whetherTable",
          scopedSlots: { customRender: "whetherTable" },
        },
        {
          title: this.$t("COLUMN_FIELD_OMISSION"),
          align: "center",
          dataIndex: "whetherRetract",
          scopedSlots: { customRender: "whetherRetract" },
        },
        {
          title: this.$t("UNIQUE_CODE"),
          align: "center",
          dataIndex: "whetherCode",
          scopedSlots: { customRender: "whetherCode" },
        },
        // {
        //   title: "增改",
        //   align: "center",
        //   dataIndex: "whetherAddUpdate",
        //   scopedSlots: { customRender: "whetherAddUpdate" },
        // },
        {
          title: this.$t("REQUIRED"),
          align: "center",
          dataIndex: "whetherRequired",
          scopedSlots: { customRender: "whetherRequired" },
        },
        {
          title: this.$t("IS_QUERY"),
          align: "center",
          dataIndex: "queryWhether",
          scopedSlots: { customRender: "queryWhether" },
        },
        {
          title: this.$t("QUERY_MODE"),
          dataIndex: "queryType",
          scopedSlots: { customRender: "queryType" },
        },
      ],
      indexConfigShow: false,
      tableLoading: false,
      visible: false,
      loadData: [],
      javaTypeData: [],
      effectTypeData: [],
      dictDataAll: [],
      codeGenQueryTypeData: [],
      yesOrNoData: [],
      id: "",
    };
  },
  computed: {},
  mounted() {
    this.id = this.$route.query.id || "";
    this.queryDictData();
    this.getAllDicData();
    this.getData();
  },
  methods: {
    queryDictData() {
      return Promise.all([
        this.getDictByCode(`CODEGENERATE:JAVA_TYPE`), // java类型
        this.getDictByCode(`CODEGENERATE:EFFECT_TYPE`), // 作用类型
        this.getDictByCode(`CODEGENERATE:QUERY_TYPE`), // 查询方式
      ]).then(([javaTypeRes, effectTypeRes, queryTypeRes]) => {
        this.javaTypeData = javaTypeRes.data.data;
        this.effectTypeData = effectTypeRes.data.data;
        this.codeGenQueryTypeData = queryTypeRes.data.data;
      });
    },
    // 获取所有数据字典数据
    getAllDicData() {
      this.$axios
        .get(
          `${ApiConstant.PREFIX_CORE}/api/sys/dict/search?page=1&size=1000`,
          {}
        )
        .then((res) => {
          this.dictDataAll = res.data?.data?.records;
        });
    },
    // 获取数据
    getData() {
      this.$axios
        .get(
          `${ApiConstant.PREFIX_CORE}/api/dev/code-generate-config/list-by-code-gen-id/${this.id}`,
          {}
        )
        .then((res) => {
          if (res.data?.data && res.data?.data.length > 0) {
            /* eslint-disable */
            res.data?.data.map((item) => {
              item.whetherTable = item.whetherTable !== "N";
              item.whetherRetract = item.whetherRetract !== "N";
              item.whetherAddUpdate = item.whetherAddUpdate !== "N";
              item.whetherRequired = item.whetherRequired !== "N";
              item.whetherCode = item.whetherCode !== "N";
              item.queryWhether = item.queryWhether !== "N";
              item.whetherCommon = item.whetherCommon !== "N";
              return item;
            });
            /* eslint-enable */
            this.loadData = res.data?.data;
          }
        });
    },
    /**
     * 打开界面
     */
    // open (data) {
    //   this.indexConfigShow = true
    //   this.tableLoading = true
    //   const dictOption = this.$options
    //   this.javaTypeData = dictOption.filters['dictData']('code_gen_java_type')
    //   this.effectTypeData = dictOption.filters['dictData']('code_gen_effect_type')
    //   this.dictDataAll = dictOption.filters['dictDataAll']()
    //   this.yesOrNoData = dictOption.filters['dictData']('yes_or_no')
    //   this.codeGenQueryTypeData = dictOption.filters['dictData']('code_gen_query_type')
    //   const params = {
    //     codeGenId: data.id
    //   }
    //   sysCodeGenerateConfigList(params).then((res) => {
    //     this.loadData = res.data
    //     this.loadData.forEach(item => {
    //       for (const key in item) {
    //         if (item[key] === 'Y') {
    //           item[key] = true
    //         }
    //         if (item[key] === 'N') {
    //           item[key] = false
    //         }
    //       }
    //     })
    //     this.tableLoading = false
    //   })
    // },
    /**
     * 判断是否（用于是否能选择或输入等）
     */
    judgeColumns(data) {
      if (
        data.columnName.includes("CREATE_BY") ||
        data.columnName.includes("CREATE_TIME") ||
        data.columnName.includes("UPDATE_BY") ||
        data.columnName.includes("UPDATE_TIME") ||
        data.columnName.includes("REVISION") ||
        data.columnName.includes("DEL_FLAG") ||
        data.columnKey === "PRI"
      ) {
        return true;
      }
      return false;
    },
    handleCancel() {
      this.$store.dispatch("tagsView/jumpBeforeView", {
        to: { path: "/codegenerate" },
        vm: this,
      });
    },
    handleConfirm() {
      const loadDatas = JSON.parse(JSON.stringify(this.loadData));
      loadDatas.forEach((item) => {
        /* eslint-disable */
        // 必填那一项转换
        for (const key in item) {
          if (item[key] === true) {
            item[key] = "Y";
          }
          if (item[key] === false) {
            item[key] = "N";
          }
        }
        /* eslint-enable */
      });
      const param = loadDatas;
      this.$axios
        .put(`${ApiConstant.PREFIX_CORE}/api/dev/code-generate-config`, param)
        .then((res) => {
          this.$message.success(this.$t("SAVE_SUCCESS"));
          this.handleCancel();
        });
    },
  },
};
</script>
<style lang="less" scoped>
.ami-core-icon {
  margin-top: -3px;
  vertical-align: middle;
}
</style>
