<template>
  <div class="app-container">
    <card-table :modal-data="tableData" :searchParams="queryParams" @pagination="getList" :loading="loading"
                :tableSearch="true" @columnSearch="handleQuery" @resetList="resetQuery">
      <template #operations="{selections}">
        <Row :gutter="20">
          <el-col :span="1.5">
            <CustomButton label="validation.addRule" @handleClick="toAdd"/>
          </el-col>
        </Row>
      </template>

      <Column type="index" label="No"/>
      <!--      规则名称-->
      <Column :label="$t('validation.ruleName')" prop="caseName" :searchParams="queryParams" :showSearch="true"/>
      <Column :label="$t('validation.formulaSetting')" prop="conditionFormula"/>
      <Column :label="$t('validation.status')" prop="status" :min-width="80">
        <template #default="{row}">
          <el-switch v-model="row.status" :active-value="'0'" :inactive-value="'1'"
                     @change="val=> handleRowStatus(val,row)"/>
        </template>
      </Column>
      <Column :label="$t('validation.relationElevatorNum')" prop="relationNums"/>
      <Column :label="$t('validation.updateBy')" prop="updateByName" :min-width="80"/>
      <Column :label="$t('validation.updateTime')" prop="updateTime"/>
      <!--      <Column :label="$t('validation.createBy')" prop="createByName" :min-width="80"/>-->
      <!--      <Column :label="$t('validation.createTime')"  :min-width="200" :searchParams="queryParams"-->
      <!--              :showSearch="true" searchType="dateRange">-->
      <!--        <template #default="{row}">-->
      <!--           {{parseTime(row.createTime) }}-->
      <!--        </template>-->
      <!--      </Column>-->
      <Column :label="$t('common.operate')" :showSearch="true" search-type="operate">
        <template #default="{row}">
          <CustomLink label="common.view" @handleClick="toView(row)"/>
          <CustomLink label="validation.seeElevator" @handleClick="toViewElevator(row)"/>
          <CustomLink label="common.edit" @handleClick="toEdit(row)"/>
          <CustomLink label="common.delete" @handleClick="deleteRow(row)"/>
        </template>
      </Column>


    </card-table>

    <Dialog ref="seeElevator" :title="$t('validation.seeElevator')" @close="dialogParams={  pageNum: 1,
        pageSize: 10,},dialogModalData=[]">
      <CardTable :modal-data="dialogModalData" :searchParams="dialogParams" @pagination="getDialogList"
                 :loading="dialogModalLoading">
        <Column type="index" label="No"/>
        <Column :label="$t('elevatorModel.areaName')" prop="areaName"/>
        <Column label="区域（英）" prop="areaNameES"/>
        <Column label="区域（俄）" prop="areaNameRU"/>
        <Column label="区域（西）" prop="areaNameUS"/>
        <Column :label="$t('elevatorModel.elevatorModel')" prop="elevatorModel"/>
      </CardTable>
    </Dialog>


  </div>
</template>

<script>

import CardTable from "../../../components/element/table/CardTable.vue";
import Row from "../../../components/CustomElement/layout/Row.vue";
import Col from "../../../components/CustomElement/layout/Col.vue";
import CustomButton from "../../../components/CustomElement/CustomButton.vue";
import Column from "../../../components/element/table/Column.vue";
import {
  changeValidationStatus,
  deleteValidation,
  getRelationByIdValidation,
  listValidation
} from "../../../api/business/crm/paraValidation/paraValidation";
import CustomLink from "../../../components/CustomElement/CustomLink.vue";
import {parseTime} from "../../../utils/dateUtils";

export default {
  name:"ValidationConfig",
  components: {CustomLink, Column, CustomButton, Col, Row, CardTable},
  data() {
    return {
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      loading: false,
      dialogModalData: [],
      dialogModalLoading: false,
      dialogParams: {
        pageNum: 1,
        pageSize: 10,
      }
    };
  },
  computed: {},
  watch: {},
  methods: {
    parseTime,
    toViewElevator(row) {
      this.$refs.seeElevator.open();
      this.$set(this.dialogParams, 'id', row.id);
      this.getDialogList()
    },

    getDialogList(val) {
      this.dialogModalLoading = true;
      getRelationByIdValidation({
        ...this.dialogParams,
      }).then(res => {
        if (res && res.rows) {
          this.dialogModalData = res.rows
          this.dialogParams.total = parseInt(res.total);
        }

      }).finally(() => {
        this.dialogModalLoading = false;

      })

    },

    getList() {
      this.loading = true;
      listValidation(this.queryParams).then(res => {
        this.tableData = res.rows;
        this.total = res.total;
        this.queryParams.total = res.total;
      }).finally(() => {
        this.loading = false;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      }
      this.handleQuery();
    },
    toAdd() {
      this.$tab.openPage(this.$t('validation.addRule'), '/param/validationConfig/secondaryPage/0', {type: 1})
    },
    toView(row) {
      this.$tab.openPage(this.$t('validation.detailRule'), '/param/validationConfig/secondaryPage/' + row.id, {
        type: 4,
        id: row.id
      })
    },
    toEdit(row) {
      this.$tab.openPage(this.$t('validation.editRule'), '/param/validationConfig/secondaryPage/' + row.id, {
        type: 2,
        id: row.id
      })
    },
    deleteRow(row) {
      this.$confirm(this.$t('common.isDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.yes'),
        cancelButtonText: this.$t('common.no'),
        type: 'warning'
      }).then(() => {
        deleteValidation({id: row.id, updateTime: row.updateTime}).then(res => {
          if (res) {
            this.$message.success(this.$t('common.deleteSuccess'));
            this.handleQuery();
          }
        })
      })
    },
    handleRowStatus(val, row) {
      let msg = row.status === '1' ? this.$t('validation.enbaleConfrim') : this.$t('validation.enbaleConfrim');
      this.$confirm(msg, this.$t('common.tips'), {
        confirmButtonText: this.$t('common.yes'),
        cancelButtonText: this.$t('common.no'),
        type: 'warning'
      }).then(() => {
        changeValidationStatus({id: row.id, status: row.status, updateTime: row.updateTime}).then(res => {
          if (res) {
            let operationMsg = row.status === '1' ? this.$t('common.deactivatSuccess') : this.$t('common.enableSuccess');
            this.$message.success(operationMsg);
            this.handleQuery();
          }
        })
      }).catch(() => {
        this.$set(row, 'status', val === '1' ? '0' : '1');
      })
    }
  },


  created() {

  },
  mounted() {
    this.handleQuery();
  },


}


</script>

<style scoped>

</style>

