<template>
  <div class="tenant">
    <byTable :hideTable="true" :hidePagination="true" :source="sourceList.data" :pagination="sourceList.pagination" :config="config"
             :selectConfig="selectConfig" highlight-current-row :onMoreSearch="true" @moreSearch="clickMoreSearch" @get-list="getList">
    </byTable>
    <div style="padding: 0 20px 20px 20px; background-color: white">
      <el-table v-loading="loading" :data="sourceList.data">
        <el-table-column label="合同编号" prop="code" width="160" fixed />
        <el-table-column label="客户名称" prop="customerName" min-width="200" fixed />
        <el-table-column label="业务员" prop="userName" width="120" fixed />
        <el-table-column label="合同金额" width="120" fixed="">
          <template #default="{ row }">
            <div>{{ row.currency }} {{ moneyFormat( row.amount,2) }}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否已结清" width="120" fixed>
          <template #default="{ row }">
            <div> {{ dictValueLabel(row.isSettled, isSettled) }}</div>
          </template>
        </el-table-column> -->
        <el-table-column label="是否已结清" width="120" fixed>
          <template #default="{ row }">
            <div>
              <span style="padding: 4px" :class="[row.isSettled == 1 ? 'active' : '']">
                {{
                proxy.dictValueLabel(row.isSettled, isSettled)
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="统计" fixed>
          <el-table-column label="收入合计" prop="incomeAmount" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.incomeAmount ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="支出合计" prop="expenditureAmount" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.expenditureAmount ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="毛利" prop="gross" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.gross ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="毛利率" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.gross ,2) }}%</div>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="收入">
          <el-table-column label="到账金额" prop="sumClaimMoney" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.sumClaimMoney ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="退税金额" prop="refundableAmount" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.refundableAmount ,2) }}</div>
            </template></el-table-column>
          <el-table-column label="其他收入" prop="otherIncomeAmount" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.otherIncomeAmount ,2) }}</div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="采购合同金额" prop="sumPurchaseContractMoney" width="120">
          <template #default="{ row }">
            <div> {{ moneyFormat(row.sumPurchaseContractMoney ,2) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="支出">
          <el-table-column label="已付货款" prop="accountPaid" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.accountPaid ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="代理费" prop="agencyFee" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.agencyFee ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="拖车费" prop="trailerFee" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.trailerFee ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="报关费" prop="customsFee" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.customsFee ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="港杂费" prop="portMixedFee" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.portMixedFee ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="验货红包" prop="inspectionRedPack" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.inspectionRedPack ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="佣金" prop="commission" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.commission ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="检测费" prop="checkout" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.checkout ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="验货费" prop="inspectionCharge" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.inspectionCharge ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="运费" prop="freight" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.freight ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="产地证费" prop="certificateOfOrigin" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.certificateOfOrigin ,2) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="其他" prop="other" width="120">
            <template #default="{ row }">
              <div> {{ moneyFormat(row.other ,2) }}</div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="备注" prop="remark" width="200" />
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="{ row }">
            <div>
              <el-button type="primary" @click="changeAgencyFee(row)" link>代理费</el-button>
              <el-button type="primary" @click="changeRemark(row)" link>备注</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="padding: 20px" justify="end" type="flex">
        <el-pagination background layout="total, sizes, prev, pager, next, jumper" :current-page="sourceList.pagination.pageNum"
                       :page-size="sourceList.pagination.pageSize" :total="sourceList.pagination.total" @size-change="handleSizeChange"
                       @current-change="handlePageChange" />
      </el-row>
    </div>

    <el-dialog title="代理费" v-if="openAgencyFee" v-model="openAgencyFee" width="400">
      <byForm :formConfig="formAgencyFeeConfig" :formOption="formOption" v-model="formAgencyFeeData.data" ref="agencyFee">
        <template #agencyFee>
          <div style="width: 100%">
            <el-form-item label="代理费" prop="agencyFee">
              <el-input-number onmousewheel="return false;" v-model="formAgencyFeeData.data.agencyFee" :precision="2" :controls="false" :min="0" />
            </el-form-item>
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="openAgencyFee = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitAgencyFeeForm()" size="large">确 定</el-button>
      </template>
    </el-dialog>

    <el-dialog title="备注" v-if="openRemark" v-model="openRemark" width="400">
      <byForm :formConfig="formRemarkConfig" :formOption="formOption" v-model="formRemarkData.data" ref="remark">
        <template #remark>
          <div style="width: 100%">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formRemarkData.data.remark" :rows="4" type="textarea" />
            </el-form-item>
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="openRemark = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitRemarkForm()" size="large">确 定</el-button>
      </template>
    </el-dialog>

    <el-dialog :title="'高级检索'" v-model="moreSearchDialog" width="500px" destroy-on-close>
      <byForm :formConfig="formSearchConfig" :formOption="formOption" v-model="sourceList.pagination">
      </byForm>
      <template #footer>
        <el-button @click="moreSearchReset" size="large">重置</el-button>
        <el-button @click="moreSearchQuery" type="primary" size="large">搜索</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import useUserStore from "@/store/modules/user";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const accountCurrency = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    userId: "",
    userName: "",
    contractCode: "",
    customerName: "",
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const isSettled = ref([
  {
    label: "已结清",
    value: "1",
  },
  {
    label: "未结清",
    value: "0",
  },
]);
const selectConfig = computed(() => {
  return [
    {
      label: "是否已结清",
      prop: "isSettled",
      data: isSettled.value,
    },
  ];
});

const config = computed(() => {
  return [];
});
const getDict = () => {
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "account_currency",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        accountCurrency.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy
    .post("/contract/getProfitClearingPage", sourceList.value.pagination)
    .then((res) => {
      sourceList.value.data = res.rows;
      sourceList.value.pagination.total = res.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
};
getDict();
getList();
const handleSizeChange = (val) => {
  sourceList.value.pagination.pageNum = 1;
  sourceList.value.pagination.pageSize = val;
  getList();
};
const handlePageChange = (val) => {
  sourceList.value.pagination.pageNum = val;
  getList();
};
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const openAgencyFee = ref(false);
const agencyFee = ref(null);
const formAgencyFeeData = reactive({
  data: {},
});
const formAgencyFeeConfig = computed(() => {
  return [
    {
      type: "slot",
      slotName: "agencyFee",
    },
  ];
});
const changeAgencyFee = (row) => {
  formAgencyFeeData.data = {
    id: row.id,
    agencyFee: row.agencyFee,
  };
  openAgencyFee.value = true;
};
const submitAgencyFeeForm = () => {
  proxy.post("/contract/edit", formAgencyFeeData.data).then(() => {
    ElMessage({
      message: "保存成功",
      type: "success",
    });
    openAgencyFee.value = false;
    getList();
  });
};
const openRemark = ref(false);
const remark = ref(null);
const formRemarkData = reactive({
  data: {},
});
const formRemarkConfig = computed(() => {
  return [
    {
      type: "slot",
      slotName: "remark",
    },
  ];
});
const changeRemark = (row) => {
  formRemarkData.data = {
    id: row.id,
    remark: row.remark,
  };
  openRemark.value = true;
};
const submitRemarkForm = () => {
  proxy.post("/contract/edit", formRemarkData.data).then(() => {
    ElMessage({
      message: "保存成功",
      type: "success",
    });
    openRemark.value = false;
    getList();
  });
};

const formSearchConfig = computed(() => {
  return [
    {
      type: "input",
      label: "业务员",
      prop: "userName",
      itemWidth: 100,
    },
    {
      type: "input",
      label: "合同编号",
      prop: "contractCode",
      itemWidth: 100,
    },
    {
      type: "input",
      label: "客户名称",
      prop: "customerName",
      itemWidth: 100,
    },
    {
      type: "date",
      itemType: "datetime",
      label: "合同时间",
      prop: "beginTime",
      placeholder: "合同开始时间",
      itemWidth: 50,
      clearable: true,
    },
    {
      type: "date",
      itemType: "datetime",
      label: " ",
      prop: "endTime",
      placeholder: "合同结束时间",
      itemWidth: 50,
      clearable: true,
    },
  ];
});
const moreSearchDialog = ref(false);
const clickMoreSearch = () => {
  moreSearchDialog.value = true;
};
const moreSearchQuery = () => {
  moreSearchDialog.value = false;
  getList();
};
const moreSearchReset = () => {
  sourceList.value.pagination = {
    total: 0,
    pageNum: sourceList.value.pagination.pageNum,
    pageSize: sourceList.value.pagination.pageSize,
    keyword: "",
    userId: "",
    userName: "",
    contractCode: "",
    customerName: "",
    beginTime: "",
    endTime: "",
  };
  moreSearchQuery();
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}

.active {
  background: #a6dd82;
  color: #fff;
  border-radius: 4px;
}
</style>
