<!--
 * @Author: zhp
 * @Date: 2023-04-17 14:23:17
 * @LastEditTime: 2023-07-14 15:56:32
 * @LastEditors: DY
 * @Description: 
-->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__user">
      <SearchBar :formConfigs="formConfig" ref="ruleForm" @headBtnClick="buttonClick">
        <el-radio-group v-model="ment" size="small">
          <el-radio-button label="">{{ $t('all') }} </el-radio-button>
          <el-radio-button label="0">{{ $t('notAvailable') }} </el-radio-button>
          <el-radio-button label="1">{{ $t('available') }} </el-radio-button>
        </el-radio-group>
      </SearchBar>
      <base-table id="palletTable" :table-props="tableProps" :page="listQuery.page" ref="palletTable1"
        @emitFun="inputChange"  :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">
        <supplier-add ref="addOrUpdate" @refreshDataList="successSubmit">
        </supplier-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">
        <supplierProduct-search ref="searchOrUpdate" @successSubmit="conditionSearchSubmit">
        </supplierProduct-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-card>
</template>

<script>
import basicPage from "@/mixins/basic-page"
import basicSearch from "@/mixins/basic-search"
import supplierProductSearch from "./components/supplierProductSearch.vue"
import supplierAdd from "./components/supplier-add.vue"
import inputArea from "./components/inputArea"
import innerTable from "./components/innerTable"
import changeStatus from "./components/changeStatus.vue"
import i18n from "@/i18n"
import status from './components/status.vue'
// import i18n from "@/i18n";
import supplier from '@/filters/supplier'

const tableProps = [
  {
    prop: "detail",
    label:'',
    subcomponent: innerTable,
  },
  {
    prop: "code",
    label: i18n.t('supplier.code'),
  },
  {
    prop: "name",
    label: i18n.t('supplier.name'),
    subcomponent: inputArea,
  },
  {
    prop: "abbreviation",
    label: i18n.t('supplier.abbreviation'),
  },
  {
    prop: "grade",
    label: i18n.t('supplier.grade'),
  },
  {
    prop: "address",
    label: i18n.t('supplier.address'),
  },
  {
    prop: "supplierStatus",
    // label: i18n.t('supplier.ment'),
    // subcomponent: status,
    label: i18n.t('supplier.supplierStatus'),
    filter: supplier('supplierStatus'),
  },
  {
    prop: "ment",
    label: i18n.t('supplier.ment'),
    subcomponent: changeStatus
  }
];
const tableBtn = [
  {
    type: "edit",
    btnName: i18n.t('t.edit'),
  },
  {
    type: "delete",
    btnName: i18n.t('t.delete'),
  }
];
export default {
  mixins: [basicPage, basicSearch],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/supplier/qmsSupplier/page",
        deleteURL: "/supplier/qmsSupplier",
        submitURL: "/supplier/qmsSupplier",
        exportURL: '/supplier/qmsSupplier/export',
        getSupplierTypeListURL: '/supplier/qmsSupplierType/page'
        // submitURL: '/supplier/qmsSupplierType'
      },
      tableProps,
      ment: '',
      tableBtn,
      productData: {},
      dataForm:{
        limit: 999,
        page:1
      },
      searchOrEditTitle: "",
      searchOrUpdateVisible: false,
      productOrEditTitle: "",
      addOrEditTitle: '',
      addOrUpdateVisible: false,
      productOrUpdateVisible: false,
      formConfig: [
        {
          type: 'input',
          label: i18n.t('supplier.name'),
          placeholder: i18n.t('supplier.name'),
          param: 'supplierName'
        },
        {
          type: 'input',
          label: i18n.t('supplier.abbreviation'),
          placeholder: i18n.t('supplier.abbreviation'),
          param: ''
        },
        {
          type: 'select',
          label: i18n.t('supplier.supplierType'),
          selectOptions: [],
          param: 'supplierTypeId',
          clearable: true,
          filterable: true,

        },
        {
          type: 'select',
          label: i18n.t('supplier.ment'),
          selectOptions: [
            { id: 0, name: '合格' },
            { id: 1, name: '不合格' },
            { id: 2, name: '黑名单' }
          ],
          param: 'supplierStatus',
          clearable: true,
          filterable: true
        },
        // {
        //   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: "success",
          plain: true
        },
        {
          type: "button",
          btnName: i18n.t('export'),
          name: "export",
          color: "primary",
          plain: true
        }
      ],
    };
  },
  components: {
    supplierProductSearch,
    // supplierProduct,
    supplierAdd
  },
  mounted () {
    this.getData();
  },
  methods: {
    inputChange(val) {
      console.log('=======')
      console.log(val)
      this.tableData[val._pageIndex - 1][val.prop] = val[val.prop]
      console.log(this.tableData)
      this.$http.put(this.urlOptions.submitURL, this.tableData[val._pageIndex - 1])
        .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.getDataList()
            },
          });
        })
    },
    // emitButtonClick() {
    //   console.log('emitButtonClick')
    //   let obj = {}
    //   for (let i of this.tableData) {
    //     obj[i.prop] = ''
    //   }
    //   this.tableData.push(obj)
    // },
    getData() {
      this.$http
        .get(this.urlOptions.getSupplierTypeListURL, { params: this.listQuery })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.formConfig[2].selectOptions = res.data.list
          }
        })
        .catch(() => {
        })
    },
    //search-bar点击
    handleProductCancel() {
      this.productOrUpdateVisible = false;
      this.productOrEditTitle = "";
    },
    handleCurrentChange(val) {
      console.log(val);
      this.productData = val.newVal ? val.newVal : {}
    },
    setCurrent(index) {
      this.$refs.palletTable1.setCurrent("palletTable", index);
    },
    // 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.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);
        });
      }
    },
    // addOrUpdateHandle(productData) {
    //   this.addOrUpdateVisible = true;
    //   this.$nextTick(() => {
    //     this.$refs.addOrUpdate.init(productData);
    //   });
    // },
    buttonClick(val) {
      console.log(val)
      switch (val.btnName) {
        case "search":
          // this.listQuery.paramCode = val.paramCode;
          this.listQuery.supplierName = val.supplierName ? val.supplierName : undefined
          this.listQuery.supplierStatus = val.supplierStatus ? val.supplierStatus : undefined
          this.listQuery.supplierTypeId = val.supplierTypeId ? val.supplierTypeId : undefined
          this.listQuery.ment = this.ment ? this.ment : undefined
          // console.log(i18n);
          this.listQuery.page = 1;
          this.getDataList();
          break;
        case "export":
          // this.listQuery.paramCode = val.paramCode;
          this.exportHandle()
          break;
        case "add":
          this.addOrEditTitle = '新增'
          this.addOrUpdateVisible = true;
          this.addOrUpdateHandle()
          break;
        default:
      }
    },
  },
};
</script>
