<template>
    <div>
        <PageTable ref="vDefineTable" action="VSelfDefineList" :columns="tableColumns" :initParam="{vsCode:this.vsCode}">
            <Form :model="formSearch" slot="search-bar" label-position="top" class="ivu-form-no-margin-bottom" inline>
                <Form-item :label="this.$t('module.valueSet.vsCode')">
                    <Input type="text" v-model="formSearch.vsCode" readonly></Input>
                </Form-item>
                <Form-item :label="this.$t('module.valueSet.vsName')">
                    <Input type="text" v-model="formSearch.vsName" readonly ></Input>
                </Form-item>
                <Form-item :label="this.$t('module.valueSet.vsValueSource')">
                    <Input type="text" v-model="formSearch.vsValueSource" readonly></Input>
                </Form-item>
                <Form-item :label="this.$t('common.status')">
                    <Input type="text" v-model="formSearch.enabled" readonly></Input>
                </Form-item>
            </Form>
            <Form :model="formSearch" slot="search-bar" label-position="top" class="ivu-form-no-margin-bottom" inline>
                <Form-item :label="this.$t('module.userManage.affiliation')">
                    <Input type="text" v-model="formSearch.orgName" readonly></Input>
                </Form-item>
                <Form-item :label="this.$t('module.valueSet.vsType')">
                    <Input type="text" v-model="formSearch.vsType" readonly></Input>
                </Form-item>
                <Form-item :label="this.$t('module.valueSet.parentVs')">
                    <Input type="text" v-model="formSearch.vsParentName" readonly></Input>
                </Form-item>
            </Form> 
            <!-- <Form :model="formSearch" slot="search-bar" label-position="top" class="ivu-form-no-margin-bottom" inline>
            </Form> -->
            <div slot="table-bar">
                <OptionRight resourceCode="items_add">
                    <Button type="ghost" icon="plus-round" size="small" @click="addOrUpdateVDefineSelf">{{$t("common.create")}}</Button>
                </OptionRight>
                <OptionRight resourceCode="dict_item_del">
                    <Button type="ghost" icon="trash-a" size="small" @click="deleteVDefineSelf">{{$t("common.delete")}}</Button>
                </OptionRight>
            </div>
        </PageTable>
    </div>
</template>
<script>
import AddOrUpdateVDefineSelf from "./AddOrUpdateVDefineSelf.vue";
export default {
  components: {},
  props: {
    vsId: {
      type: String,
      default() {
        return "";
      }
    },
    vsCode: {
      type: String,
      default() {
        return "";
      }
    },
    columnType: {
        type: String,
        default() {
        return "";
        }
    }
  },
  data() {
    return {
      formSearch: {
        vsCode: "",
        vsName: "",
        orgName: "",
        vsParentName: "",
        vsType: "",
        vsValueSource: "",
        enabled: ""
      },
    //   tableColumns: [],
      baseColumns: [
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: this.$t("common.operating"),
          align: "center",
          width: 120,
          render: (h, params) => {
            return h("RowTools", {
              props: {
                data: [
                  {
                    type: "icon",
                    icon: "md-create",
                    text: this.$t("common.edit"),
                    handle: () => {
                      this.addOrUpdateVDefineSelf(params.row);
                    }
                  }
                ]
              }
            });
          }
        },
        {
          title: this.$t("module.valueSet.key"),
          align: "center",
          key: "vsiKey"
        },
        {
          title: this.$t("module.valueSet.value"),
          align: "center",
          key: "vsiValue"
        },
        // {
        // title: this.$t('module.valueSet.parentValue'),
        // align: "center",
        // key: "vsiParentValue",
        // visible: false
        // },
        // this.getParentVsColumn(),
        {
          title: this.$t("module.valueSet.startDate"),
          align: "center",
          key: "vsiStartDate",
          render: (h, params) => {
            return h("format", {
              props: {
                type: "date",
                value: params.row.vsiStartDate,
                param: "yyyy-MM-dd"
              }
            });
          }
        },
        {
          title: this.$t("module.valueSet.endDate"),
          align: "center",
          key: "vsiEndDate",
          render: (h, params) => {
            return h("format", {
              props: {
                type: "date",
                value: params.row.vsiEndDate,
                param: "yyyy-MM-dd"
              }
            });
          }
        },
        {
          title: this.$t("common.status"),
          align: "center",
          key: "enabled",
          render: (h, params) => {
            return h(
              "Tag",
              {
                props: {
                  type: "dot",
                  color: params.row.enabled == 1 ? "blue" : "red"
                }
              },
              params.row.enabled == 1
                ? this.$t("common.valid")
                : this.$t("common.invalid")
            );
          }
        }
      ]
    };
  },
  computed: {
    tableColumns() {
        $store.dispatch("GetValueSetInfo", this.vsId).then(response => {
            this.formSearch.vsCode = response.dataResult.vsCode;
            this.formSearch.vsName = response.dataResult.vsName;
            this.formSearch.vsType =
            response.dataResult.vsType == "1"
                ? this.$t("module.valueSet.independent")
                : this.$t("module.valueSet.subordinate");
            this.formSearch.vsParentName = response.dataResult.vsParentName;
            this.formSearch.orgName = response.dataResult.orgName;
            this.formSearch.enabled =
            response.dataResult.enabled == "1"
                ? this.$t("common.valid")
                : this.$t("common.invalid");
            this.formSearch.vsValueSource =
            response.dataResult.vsValueSource == "1"
                ? this.$t("module.valueSet.userDefine")
                : this.$t("module.valueSet.dynamicSql");
        });
        if (this.columnType == "2") {
            return [
                ...this.baseColumns,
                {
                    title: this.$t('module.valueSet.parentValue'),
                    align: "center",
                    key: "vsiParentValue",
                },
            ]
        }else {
            return this.baseColumns;
        }
    },                                                                                            
  },
  created: function() {},
  beforeMount: function() {},
  mounted: function() {},
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
    addOrUpdateVDefineSelf(obj) {
      new this.$pageModal(
        AddOrUpdateVDefineSelf,
        {
          props: {
            currentVsCode: this.formSearch.vsCode,
            vsId: obj.vsiId ? obj.vsiId : "",
            vsType: this.formSearch.vsType
          }
        },
        data => {
          this.$refs["vDefineTable"].load({vsCode:this.vsCode});
        }
      );
    },
    deleteVDefineSelf() {
      let deleteStr = this.getIdAndVersion();
      if (deleteStr == "") {
        this.$Message.warning({
          content: this.$t("module.valueSet.warning.selestItems"),
          duration: 3
        });
        return;
      }
      new $confirm(`${this.$t("common.confirmDeletion")}`, this).then(() => {
        $store
          .dispatch("DeleteVDefineSelf", this.getIdAndVersion())
          .then(res => {
            if (res.statusCode == "0") {
              this.$Message.success({
                content: this.$t("module.valueSet.warning.deleteSuccessfully"),
                duration: 3
              });
              this.$refs["vDefineTable"].load({vsCode:this.vsCode});
            } else {
              this.$Message.warning({
                content: res.msg,
                duration: 3
              });
              return;
            }
          });
      });
    },
    getIdAndVersion() {
      let list = this.$refs["vDefineTable"].getSelectioned() || [];
      let ids = [];
      for (let vs of list) {
        ids.push({ vsiId: vs.vsiId, version: vs.version });
      }
      return ids;
    },
    getParentVsColumn() {
      if (this.columnType == "2") {
        return {
          title: this.$t("module.valueSet.parentValue"),
          align: "center",
          key: "vsiParentValue",
          visible: false
        };
      } else {
        return;
      }
    },
  },
  watch: {},
  directives: {}
};
</script>
