<!--
 * @Author: zhp
 * @Date: 2023-02-21 14:30:17
 * @LastEditTime: 2023-06-26 15:32:36
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <el-row :gutter="24">
      <el-col :span="14">
        <div class="mod-sys__user">
          <SearchBar :formConfigs="formConfig" ref="ruleForm" @headBtnClick="buttonClick">
            <el-badge :value="2" class="item">
              <el-button type="primary" size="small" @click="conditionSearch">{{ $t("conditionSearch")}}}</el-button>
            </el-badge>
          </SearchBar>
          <base-table highlight-current-row id="palletTable" :table-props="tableProps" :page="listQuery.page"
            @current-change="handleCurrentChange" ref="palletTable1" :limit="listQuery.limit" :table-data="tableData">
            <method-btn v-if="tableBtn.length" slot="handleBtn" :width="100" label="操作" :method-list="tableBtn"
              @clickBtn="handleClick" />
          </base-table>
          <pagination :limit.sync="listQuery.limit" :page.sync="listQuery.page" :total="listQuery.total"
            @pagination="getDataList" />
          <!-- 弹窗, 新增 / 修改 -->
          <base-dialog :dialogTitle="addOrEditTitle" :dialogVisible="addOrUpdateVisible" @cancel="handleCancel"
            @confirm="handleConfirm" :before-close="handleCancel">
            <team-add ref="addOrUpdate" @refreshDataList="successSubmit"></team-add>
            <!-- <el-row slot="footer" type="flex" justify="end"> </el-row> -->
          </base-dialog>
          <base-dialog :dialogTitle="searchOrEditTitle" :dialogVisible="searchOrUpdateVisible"
            @cancel="handleSearchCancel" @confirm="handleSearchConfirm" :before-close="handleSearchCancel">
            <team-search ref="searchOrUpdate" @successSubmit="conditionSearchSubmit"></team-search>
            <el-row slot="footer" type="flex" justify="end">
              <el-col :span="12">
                <el-button size="small" type="primary" plain class="btnTextStyle" @click="handleSearchCancel">
                  {{ $t("close") }}
                </el-button>
                <el-button size="small" class="btnTextStyle" type="primary" plain @click="handleSearchReset">{{
                  $t("reset")
                  }}</el-button>
                <el-button type="primary" size="small" class="btnTextStyle" @click="handleSearchConfirm">
                  {{ $t("search") }}
                </el-button>
              </el-col>
            </el-row>
          </base-dialog>
        </div>
      </el-col>
      <el-col :span="10">
        <div class="mod-sys__user">
          <SearchBar :formConfigs="formConfigMember" ref="ruleForm" @headBtnClick="teamMemberButtonClick">
          </SearchBar>
          <base-table id="palletTable" :table-props="depProps" ref="palletTable1" :table-data="teamMemberData">
          </base-table>
          <!-- 弹窗, 新增 / 修改 -->
          <base-dialog :dialogTitle="teamMemberOrEditTitle" :dialogVisible="teamMemberOrUpdateVisible"
            @cancel="handleTeamMemberCancel" @confirm="handleTeamMemberConfirm" :before-close="handleTeamMemberCancel">
            <teamMember-add ref="teamMemberOrUpdate" @refreshDataList="successTeamMemberSubmit"></teamMember-add>
            <!-- <el-row slot="footer" type="flex" justify="end"> </el-row> -->
          </base-dialog>
        </div>
      </el-col>""
    </el-row>
  </el-card>
</template>

<script>
import basicPage from "@/mixins/basic-page";
import basicSearch from "@/mixins/basic-search";
import teamAdd from "./components/team-add";
import teamMemberAdd from './components/teamMember-add'
import teamSearch from "./components/teamSearch.vue";
import Cookies from 'js-cookie'
import qs from 'qs'
// import radio from "./components/radio.vue";
import i18n from "@/i18n";
const tableProps = [
  {
    prop: "teamCode",
    label: i18n.t('basic.code')
  },
  {
    prop: "teamName",
    label: i18n.t('basic.name')
  },
  {
    prop: "client",
    label: i18n.t('basic.client')
  },
  {
    prop: "remark",
    label: i18n.t('basic.remark')
  },
];
const depProps = [
  {
    prop: "departName",
    label: i18n.t('basic.departName')
  },
  {
    prop: "userName",
    label: i18n.t('basic.userName')
  }
];
const tableBtn = [
  {
    type: "edit",
    btnName: "修改"
  },
  {
    type: "delete",
    btnName: "删除"
  }
];
export default {
  mixins: [basicPage, basicSearch],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/basic/qmsTeam/page",
        deleteURL: "/basic/qmsTeam",
        exportURL: "/basic/qmsTeam/export",
        teamMemberURL: '/basic/qmsTeamMember/page'
      },
      tableProps,
      depProps,
      teamId:null,
      teamMemberData: [],
      teamMemberOrEditTitle:'',
      tableBtn,
      productData: {},
      addOrUpdateVisible: false,
      teamMemberOrUpdateVisible:false,
      searchOrEditTitle: "",
      addOrEditTitle: '',
      searchOrUpdateVisible: false,
      productOrEditTitle: "",
      productOrUpdateVisible: false,
      formConfig: [
        // {
        //   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: "导出",
          name: i18n.t('export'),
          color: "primary",
          // plain: true,
        },
        {
          type: "button",
          btnName: "取消选中",
          name: "cancel",
          color: "primary",
          // plain: true,
        },
      ],
      formConfigMember: [
        // {
        //   type: "",
        //   label: i18n.t("params.paramCode"),
        //   placeholder: i18n.t("params.paramCode"),
        //   param: "paramCode",
        // },
        // {
        //   type: "separate",
        // },
        {
          type: "button",
          btnName: i18n.t('teamMemberChange'),
          name: "change",
          color: "primary",
          // plain: true,
        }
      ],
    };
  },
  components: {
    teamAdd,
    teamMemberAdd,
    teamSearch
  },
  // mounted() {
  //   this.getDepData()
  // },
  methods: {
    //search-bar点击
    handleProductCancel() {
      this.productOrUpdateVisible = false;
      this.productOrEditTitle = "";
    },
    setCurrent(index) {
      this.$refs.palletTable1.setCurrent("palletTable", index)
      console.log(index)
    },
    handleCurrentChange(val) {
      this.teamId = val.newVal.id
      this.$http.get(this.urlOptions.teamMemberURL, {
        params: {
          limit: 999,
          page: 1,
          teamId:val.newVal.id
        }
      }).then(({ data: res } ) => {
        console.log(res)
        if (res && res.code === 0) {
          this.teamMemberData = res.data.list
        }
      })
    },
    // handleSearchCancel() {
    //   this.searchOrEditTitle = "";
    //   this.searchOrUpdateVisible = false;
    // },
    conditionSearch() {
      this.searchOrEditTitle = "搜索";
      this.searchOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.searchOrUpdate.init();
      });
    },
    // handleConfirm() {
    //   this.$refs.addOrUpdate.dataFormSubmitHandle();
    // },
    // conditionSearchSubmit() {},
    conditionSearchSubmit(dataForm) {
      // console.log(key);
      // console.log(key);
      this.listQuery.name = dataForm.name
      this.listQuery.code = dataForm.code
      this.listQuery.status = dataForm.status
      this.listQuery.page = 1;
      this.getDataList();
      this.searchOrUpdateVisible = false;
      // console.log(11111);
      // this.conditionSearchSubmit();
    },
    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(() => { })
    },
    handleClick(val) {
      console.log(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);
        });
      }
    },
    successTeamMemberSubmit() {
      this.handleTeamMemberCancel()
      this.$http.get(this.urlOptions.teamMemberURL, {
        params: {
          limit: 999,
          page: 1,
          teamId: this.teamId
        }
      }).then(({ data: res } ) => {
        if (res && res.code === 0) {
          this.teamMemberData = res.data.list
        }
      });
    },
    handleTeamMemberCancel() {
      this.teamMemberOrUpdateVisible = false
      this.teamMemberOrEditTitle = ''
    },
    teamMemberButtonClick(val) {
      console.log(val);
      switch (val.btnName) {
        case "change":
          this.teamMemberOrEditTitle = '修改团队成员'
          this.teamMemberOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.teamMemberOrUpdate.init(this.teamId)
          });
          break;
        default:
          console.log(val);
      }
    },
    handleTeamMemberConfirm() {
      this.$refs.teamMemberOrUpdate.dataFormSubmit()
    },
    buttonClick(val) {
      console.log(val);
      switch (val.btnName) {
        case "search":
          // this.listQuery.paramCode = val.paramCode;
          this.listQuery.name = null
          this.listQuery.code = null
          this.listQuery.status = null
          // console.log(i18n);
          this.listQuery.page = 1;
          this.getDataList();
          break;
        case "export":
          this.exportHandle();
          break;
        case "add":
          this.addOrEditTitle = "新增";
          this.addOrUpdateVisible = true;
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init();
          });
          break;
        case "cancel":
          this.$refs.palletTable1.setCurrent("palletTable", -1);
          break;
        // case "correlation":
        //   this.productOrEditTitle = "产品";
        //   this.productOrUpdateVisible = true;
        //   this.addOrUpdateHandle(this.productData);
        //   console.log(this.productId);
        //   break;
        // case "cancel":
        //   this.$refs.palletTable1.setCurrent("palletTable", -1);
        //   break;
        default:
          console.log(val);
      }
    },
  },
};
</script>
