<!--
 * @Author: zhp
 * @Date: 2023-04-17 14:23:17
 * @LastEditTime: 2023-06-28 14:02:19
 * @LastEditors: DY
 * @Description: 
-->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <header>评估向列表标签关联表</header>
    <!-- <div class="mod-sys__user"> -->
    <!-- <el-input v-model="name" :placeholder="$t('supplier.title')" style="width:300px" @blur="throttle(handleAdd())" @keyup.enter.native="throttle(handleAdd())">
    </el-input> -->
    <!-- </div> -->
    <!-- <el-card v-for="(item, index) in tableData" :key="index" class="box-card" style="width:80%; margin-top: 15px">
      <div slot="header" class="clearfix">
        <span>{{ item.name }}</span>
        <el-button  style="float: right; padding: 3px 0" type="text" icon="el-icon-edit" />
        <el-button style="float: right; padding: 3px 0" type="text" icon="el-icon-delete" />
      </div>
      <span v-for="(item, index) in tableData" :key="index" class="text item">
        {{'列表内容'}} {{ item.name}}
      </span>
    </el-card> -->
  </el-card>
</template>

<script>
// import basicPage from "@/mixins/basic-page"
// import basicSearch from "@/mixins/basic-search"
// import supplierProductSearch from "./components/supplierProductSearch.vue"
// import evaluationTaskAdd from "./components/evaluationTask-add.vue"
import { timeFormatter } from '@/filters'
import i18n from "@/i18n"
// import i18n from "@/i18n";
export default {
  // mixins: [basicPage],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/supplier/qmsEvaluationItemListLabel/page",
        deleteURL: "/supplier/qmsEvaluationTask",
        exportURL: '/supplier/qmsEvaluationTask/export',
        getSupplierListURL: '/supplier/qmsSupplier/page',
        getTemplateListURL: '/supplier/qmsEvaluationTask/page',
        submitURL: '/supplier/qmsEvaluationItemListLabel'
      },
      name: '',
      tableData: [],
      listQuery: {
        limit: 10,
        page: 1,
        total: 1,
      },
      searchOrEditTitle: "",
      searchOrUpdateVisible: false,
      productOrEditTitle: "",
      addOrEditTitle: '',
      addOrUpdateVisible: false,
      productOrUpdateVisible: false,
      formConfig: [
        {
          type: 'select',
          label: i18n.t('supplier.supplierName'),
          selectOptions: [],
          param: 'supplierName',
          clearable: true,
          filterable: true,
        },
        {
          type: 'select',
          label: i18n.t('supplier.evaluationTemplateName'),
          selectOptions: [],
          param: 'evaluationTemplateId',
          clearable: true,
          filterable: true,
        },
        {
          type: 'datePicker',
          label: i18n.t('supplier.startTime'),
          dateType: 'datetimerange',
          format: 'yyyy-MM-dd HH:mm:ss',
          valueFormat: 'yyyy-MM-ddTHH:mm:ss',
          rangeSeparator: '-',
          startPlaceholder: '开始时间',
          endPlaceholder: '结束时间',
          param: 'timeSlot',
          width: 350
        },
        // {
        //   type: "",
        //   label: i18n.t("params.paramCode"),
        //   placeholder: i18n.t("params.paramCode"),
        //   param: "paramCode",
        // },
        // {
        //   type: "separate",
        // },
        {
          type: "button",
          btnName: i18n.t('search'),
          name: "search",
          color: "primary",
          // plain: true,
        },
        {
          type: "button",
          btnName: i18n.t('add'),
          name: "add",
          color: "primary",
          // plain: true,
        },
        {
          type: "button",
          btnName: i18n.t('export'),
          name: "export",
          color: "primary",
          // plain: true,
        }
      ]
    };
  },
  components: {
  },
  activated() {
    this.getDataList()
  },
  methods: {
    //search-bar点击
    // handleSearchCancel() {
    //   this.searchOrEditTitle = "";
    //   this.searchOrUpdateVisible = false;
    // },
    throttle(fun, delay = 10000) {
      console.log('sssssssss')
      let last, now
      return function () {
        now = Date.now()
        if (last && now - last < delay) {
          last = now
        } else {
          last = now
          fun.call(this, ...arguments)
        }
      }
    },
    handleAdd() {
      const data = {
        name: this.name
      }
      this.$http["post"](this.urlOptions.submitURL, data)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit("successSubmit")
            },
          })
        })
    },
    conditionSearch() {
      this.searchOrEditTitle = "搜索";
      this.searchOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.searchOrUpdate.init();
      });
    },
    exportHandle() {
      this.$http.get(this.urlOptions.exportURL, { responseType: 'blob' }, {
        ...this.dataForm
      }).then((res) => {
        console.log(res)
        // if (res !== 0) {
        //   return this.$message.error(res.msg) 
        // }
        let fileName = ''
        const contentDisposition = res.headers['content-disposition']
        if (contentDisposition) {
          const temp = res.headers['content-disposition']
            .split(';')[1]
            .split('=')[1]
          // 对文件名乱码转义--【Node.js】使用iconv-lite解决中文乱码
          fileName = decodeURI(temp)
          console.log(temp)
        }
        const blob = new Blob([res.data])
        const reader = new FileReader()
        reader.readAsDataURL(blob)
        reader.onload = (e) => {
          const a = document.createElement('a')
          a.download = fileName
          a.href = e.target.result
          document.body.appendChild(a)
          a.click()
          document.body.removeChild(a)
        }
      }).catch(() => { })
    },
    // handleConfirm() {
    //   this.$refs.addOrUpdate.dataFormSubmitHandle();
    // },
    // conditionSearchSubmit() {},
    conditionSearchSubmit(dataForm) {
      // console.log(key);
      // console.log(key);
      this.listQuery.supplierName = dataForm.supplierName
      this.listQuery.page = 1;
      this.getDataList();
      this.searchOrUpdateVisible = false;
      // console.log(11111);
      // this.conditionSearchSubmit();
    },
    handleClick(val) {
      if (val.type === "delete") {
        this.$confirm(`确定对[名称=${val.data.customerTypeName}]进行删除操作?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$http.delete(this.urlOptions.deleteURL, { data: [val.data.id] }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataList();
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          })
          .catch(() => { });
      } else if (val.type === 'edit') {
        this.addOrUpdateVisible = true
        this.addOrEditTitle = this.$t('edit')
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(val.data.id);
        });
      }
    },
    buttonClick(val) {
      console.log(val)
      switch (val.btnName) {
        case "search":
          this.listQuery.page = 1;
          this.getDataList();
          break;
        case "add":
          this.addOrEditTitle = '新增'
          this.addOrUpdateVisible = true;
          this.addOrUpdateHandle()
          break;
        default:
      }
    },
    getDataList() {
      this.dataListLoading = true;
      this.$http
        .get(this.urlOptions.getDataListURL, {
          params: this.listQuery,
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.tableData = [];
            this.listQuery.total = 0;
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.listQuery.total = res.data.total;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    }
  },
};
</script>
