<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
          <el-form-item label="地点" prop="fUpdateaddress">
            <el-input style="width: 240px" v-model="queryParams.fUpdateaddress" placeholder="请选择客户类别" clearable size="small">
            </el-input>
          </el-form-item>

          <el-form-item label="箱号" prop="fNo">
            <el-input
              v-model="queryParams.fNo"
              style="width: 240px"
              placeholder="请输入名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @cell-click="modiEy" v-loading="loading" :data="corpsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" label="行号" align="center" />
      <el-table-column
        v-for="(item,index) in boxDistributionName"
        :key="item.index"
        :label="item"
        :value="item.index"
        :prop="item"
      >
      </el-table-column>
<!--      <el-table-column :show-overflow-tooltip="true" label="地点" align="center" prop="fTypename" width="200px"/>-->
<!--      <el-table-column label="20GP" align="center" prop="fNo" width="100px"/>-->
<!--      <el-table-column :show-overflow-tooltip="true" label="40GP" align="center" prop="fName" width="100px"-->
<!--      />-->
<!--      <el-table-column :show-overflow-tooltip="true" label="40HC" align="center" width="100px" prop="fCname" />-->
<!--      <el-table-column :show-overflow-tooltip="true" label="40HR" align="center" prop="fEname" />-->
<!--      <el-table-column :show-overflow-tooltip="true" label="空" align="center" prop="fTel" />-->

<!--      <el-table-column label="重" align="center" prop="createBy" />-->
<!--      <el-table-column label="好" align="center" prop="createTime" width="100"/>-->
<!--      <el-table-column label="坏" align="center" prop="updateBy" width="100px"/>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listCorps, listName, getCorps, delCorps, changeCorpsStatus,exportCorps } from "@/api/kaihe/containerNews/boxDistribution";
  import {queryUserVal} from "@/api/system/user";
  import { isArray } from '@/utils/validate'
  import Vue from 'vue'
  export default {
    name: "boxDistribution",
    components: {
    },
    data() {
      return {
        //默认显示第一行
        queryParamsHidden:false,
        //标签名
        boxDistributionName:[],


        //全屏放大
        dialogFull:false,
        contactList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户详情表格数据
        corpsList: [],
        // 弹出层标题
        title: "",
        // 状态数据字典
        statusOptions: [],
        // 是否显示弹出层
        open: false,
        // 客户类别字典
        fTypeidOptions: [],
        // 结算表票结、月结字典
        fStltypeidOptions: [],
        // 删除状态字典
        delFlagOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          fUpdateaddress:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
           fTypeid: [
            { required: true, message: "客户类别不能为空", trigger: "blur" }
          ],
          fNo: [
            { required: true, message: "编号不能为空", trigger: "blur" }
          ],
          fName: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
          fCname:[
            { required: true, message: "简称不能为空", trigger: "blur" }
          ],
          fStatus: [
            { required: true, message: "状态默认 T ,正常T 停用F 下拉选择不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("data_customer_category").then(response => {
        this.fTypeidOptions = response.data;
      });
      this.getDicts("data_stltype_type").then(response => {
        this.fStltypeidOptions = response.data;
      });
      this.getDicts("data_delete_state").then(response => {
        this.delFlagOptions = response.data;
      });
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      modiEy(row, column, cell, event){
        // console.log(row)
        console.log(column)
        // console.log(cell)
        // console.log(event)
        // console.log(column.property)
        console.log(column.label)
        console.log(row["地点"])
        let res = {
          column:column.label,
          row:row["地点"]
        }
        this.$router.push({
          path: "/containerNews/modifyPage",
          query: { data: JSON.stringify(res) },
        });
      },
      full(){
        this.dialogFull = !this.dialogFull
      },
      // 客户类别默认为客户结算方式默认月结
      query() {
        this.$set(this.form, "fStltypeid", 1);
      },
      // 添加行
      addRow(tableData,event){
        var obj = {}
         tableData.push(obj)
      },
      //删除行
      deleteRow(index, rows) {
        rows.splice(index, 1);
      },

      /** 查询客户详情列表 */
      getList() {
        this.loading = true;
        listName().then(response =>{
          this.boxDistributionName = response.rows
        })
        listCorps(this.queryParams).then(response => {
          this.corpsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 客户类别字典翻译
      fTypeidFormat(row, column) {
        return this.selectDictLabel(this.fTypeidOptions, row.fTypeid);
      },
      // 结算表票结、月结字典翻译
      fStltypeidFormat(row, column) {
        return this.selectDictLabel(this.fStltypeidOptions, row.fStltypeid);
      },
      // 删除状态字典翻译
      delFlagFormat(row, column) {
        return this.selectDictLabel(this.delFlagOptions, row.delFlag);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          fId: null,
          fTypeid: null,
          fNo: null,
          fName: null,
          fCname: null,
          fEname: null,
          fTel: null,
          fFax: null,
          fEmail: null,
          fManage: null,
          fAddr: null,
          fEaddr: null,
          fScale: null,
          fProvince: null,
          fCity: null,
          fStltypeid: null,
          fStldays: null,
          fContractno: null,
          fContractb: null,
          fContracte: null,
          fTax: null,
          fInvtel: null,
          fInvaddr: null,
          fBankno: null,
          fBankname: null,
          fUbankno: null,
          fUbankname: null,
          fStatus: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      // 从表重置
      contList() {
        this.contactList = []
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.fStatus === "0" ? "启用" : "停用";
        this.$confirm('确认要"' + text + '""' + row.fName + '"吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeCorpsStatus(row.fId, row.fStatus);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.fStatus = row.fStatus === "0" ? "1" : "0";
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.fId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.query();
        this.contList();
        this.open = true;
        this.title = "添加客户详情";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const fId = row.fId || this.ids
        getCorps(fId).then(response => {
          this.form = response.data["corp"];
          var contactList1=response.data["customerContact"];
          this.form.fTypeid = this.form.fTypeid.replace(/\[|]/g,'').replace(/\"/g, "").split(",")
          if(contactList1!=null){
            this.contactList=contactList1;
          }else {
            this.query();
          }
          this.open = true;
          this.title = "修改客户详情";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.form.fTypename = ''
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.fId != null) {
              let formDate= new FormData()
            for (let index in this.fTypeidOptions){
              for (let item in this.form.fTypeid){
                if (this.fTypeidOptions[index].dictValue == this.form.fTypeid[item]){
                  this.form.fTypename += this.fTypeidOptions[index].dictLabel + ','
                }
              }
            }
              this.form.fTypename = this.form.fTypename.substring(0,this.form.fTypename.length-1)
              formDate.append('corps',JSON.stringify(this.form));
              formDate.append('customerContacts',JSON.stringify(this.contactList));
              addCorps(formDate).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              let formDate= new FormData()
              for (let index in this.fTypeidOptions){
                for (let item in this.form.fTypeid){
                  if (this.fTypeidOptions[index].dictValue == this.form.fTypeid[item]){
                    this.form.fTypename += this.fTypeidOptions[index].dictLabel + ','
                  }
                }
              }
              this.form.fTypename = this.form.fTypename.substring(0,this.form.fTypename.length-1)
              formDate.append('corps',JSON.stringify(this.form));
              formDate.append('customerContacts',JSON.stringify(this.contactList));
              addCorps(formDate).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const fIds = row.fId || this.ids;
        this.$confirm('是否确认删除客户详情编号为"' + fIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCorps(fIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有客户详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCorps(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style lang="scss">
.avue-crud__dialog__header {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
.el-dialog__title {
  color: rgba(0,0,0,.85);
  font-weight: 500;
  word-wrap: break-word;
}
.avue-crud__dialog__menu {
  padding-right: 20px;
  float: left;
}
.avue-crud__dialog__menu i {
  color: #909399;
  font-size: 15px;
}
.el-icon-full-screen{
  cursor: pointer;
}
.el-icon-full-screen:before {
  content: "\e719";
}
</style>
