<template>
  <basic-container>
    <div class="avue-crud">
      <el-row v-if="!search">
        <!-- 查询模块 -->
        <el-form :inline="true" :model="query" class="fr-fm">
          <div class="fr-fo">
            <el-form-item label="配送车次:">
              <el-input v-model="query.trainNumber" placeholder="请输入配送车次"></el-input>
            </el-form-item>
            <el-form-item label="订单编号:">
              <el-input v-model="query.stockArticleId" placeholder="请输入订单自编号"></el-input>
            </el-form-item>
            <!--          <el-form-item label="商城名称:">-->
            <!--            <el-input v-model="query.mallName" placeholder="请输入商城名称"></el-input>-->
            <!--          </el-form-item>-->
            <!--          <el-form-item label="门店名称:">-->
            <!--            <el-input v-model="query.storeName" placeholder="请输入门店名称"></el-input>-->
            <!--          </el-form-item>-->
            <el-form-item label="客户名称:">
              <el-input v-model="query.consignee" placeholder="请输入客户名称"></el-input>
            </el-form-item>
            <el-form-item label="客户电话:">
              <el-input v-model="query.deliveryPhone" placeholder="请输入客户电话"></el-input>
            </el-form-item>
            <el-form-item label="业务类型:">
              <el-select v-model="query.businessType" placeholder="请选择业务类型">
                <el-option :key="'1'" :value="'商配'">商配 </el-option>
                <el-option :key="'2'" :value="'市配'">市配 </el-option>
              </el-select>
              <!--            <el-input v-model="query.deliveryType" placeholder="请选择业务类型"></el-input>-->
            </el-form-item>
            <el-form-item label="配送司机:">
              <!--            <el-select v-model="query.deliveryType" placeholder="请选择业务类型">-->
              <!--            </el-select>-->
              <el-select
                v-model="query.deliveryDriverName"
                filterable
                placeholder="请输入司机名称"
                :loading="loading"
              >
                <el-option
                  v-for="item in driverData"
                  :key="item.dictKey"
                  :label="item.dictValue"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select>
              <!--            <el-input v-model="query.deliveryType" placeholder="请选择业务类型"></el-input>-->
            </el-form-item>
            <!--          <el-form-item label="司机签收状态:">-->
            <!--            <el-select v-model="query.driverSigning" placeholder="请选择业务类型">-->
            <!--              <el-option-->
            <!--                      :value="1">未签收-->
            <!--              </el-option>-->
            <!--              <el-option-->
            <!--                      :value="2">已签收-->
            <!--              </el-option>-->
            <!--            </el-select>-->
            <!--            &lt;!&ndash;            <el-input v-model="query.querySigningStatus" placeholder="请选择签收状态"></el-input>&ndash;&gt;-->
            <!--          </el-form-item>-->
            <!--                        <el-form-item label="顾客姓名:">-->
            <!--                            <el-input v-model="query.customerName" placeholder="请输入服务类型"></el-input>-->
            <!--                        </el-form-item>-->
          </div>
          <!-- 查询按钮 -->
          <el-form-item class="el-btn">
            <el-button type="primary" icon="el-icon-search" @click="searchChange">搜 索</el-button>
            <el-button icon="el-icon-delete" @click="searchReset()">清 空</el-button>
          </el-form-item>
        </el-form>
      </el-row>

      <el-row>
        <div class="avue-crud__header">
          <!-- 头部左侧按钮模块 -->
          <div class="avue-crud__left"></div>
          <!-- 头部右侧按钮模块 -->
          <div class="avue-crud__right">
            <el-button icon="el-icon-refresh" @click="searchChange" circle></el-button>
            <el-button icon="Operation" @click="showdrawer(true)" circle></el-button>
            <el-button icon="el-icon-search" @click="searchHide" circle></el-button>
          </div>
        </div>
      </el-row>
      <el-row>
        <!-- 列表模块 -->
        <tablecmt
          :columnList="columnList"
          :tableData="data"
          :loading="loading"
          @inputTxt="inputsc"
          @timeCheck="timesc"
          @btnCheck="btnsc"
          @selectCheck="selectsc"
          @selection="selectionChange"
        >
          <template #default="slotProps">
            <el-text size="small" @click="editsolt(slotProps.scope)">查 看</el-text>
          </template>
        </tablecmt>
      </el-row>
      <el-row>
        <div class="avue-crud__pagination flex-c-sb" style="width: 100%">
          <div style="font-size: 14px">勾选数量: {{ selectionList.length }}</div>
          <!-- 分页模块 -->
          <el-pagination
            align="right"
            background
            @size-change="sizeChange"
            @current-change="currentChange"
            :current-page="page.currentPage"
            :page-sizes="[30, 50, 80, 120]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
          >
          </el-pagination>
        </div>
      </el-row>
      <!-- 表单模块 -->
      <el-dialog
        :title="title"
        v-model="box"
        width="50%"
        :before-close="beforeClose"
        append-to-body
      >
        <el-form :disabled="view" ref="form" :model="form" label-width="80px">
          <el-form-item label="收货人" prop="consignee">
            <el-input v-model="form.consignee" placeholder="请输入收货人" />
          </el-form-item>
          <el-form-item label="收货人手机号" prop="consigneePhone">
            <el-input v-model="form.consigneePhone" placeholder="请输入收货人手机号" />
          </el-form-item>
          <el-form-item label="收货地址" prop="deliveryAddress">
            <el-input v-model="form.deliveryAddress" placeholder="请输入收货地址" />
          </el-form-item>

          <el-form-item label="签收类型" prop="signingType">
            <el-select v-model="form.signingType" clearable placeholder="请选择签收类型">
              <el-option
                v-for="item in signingTypeData"
                :key="item.dictKey"
                :label="item.dictValue"
                :value="item.dictKey"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文员复核图片" prop="clerkSignPictures">
            <el-input v-model="form.clerkSignPictures" placeholder="请上传文员复核图片" />
          </el-form-item>
          <!--          <el-form-item label="司机签收图片" prop="deliverySignPictures">-->
          <!--            <el-input v-model="form.deliverySignPictures" placeholder="请输入司机签收图片"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="服务号" prop="serviceNumber">-->
          <!--            <el-input v-model="form.serviceNumber" placeholder="请输入服务号"/>-->
          <!--          </el-form-item>-->
          <el-form-item label="备注" prop="notes">
            <el-input v-model="form.notes" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
        <!-- 表单按钮 -->
        <template #footer>
          <span v-if="!view" class="dialog-footer">
            <el-button type="primary" icon="el-icon-circle-check" @click="handleSubmit"
              >提 交</el-button
            >
            <el-button icon="el-icon-circle-close" @click="box = false">取 消</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </basic-container>

  <edittablehead
    @setcolum="setnewcolum"
    @closce="showdrawer"
    :drawerShow="drawerShow"
    :columnList="columnList"
  ></edittablehead>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  getPage,
  getstatistics,
} from '@/api/distribution/distributionSignfor';
import option from '@/option/distribution/distributionSignfor';
import { mapGetters } from 'vuex';
import { getDictionaryBiz } from '@/api/system/dict';
import dayjs from 'dayjs';
import { getVehicleList } from '@/api/basicdata/basicdataVehicle';

import { getDriverList } from '@/api/basicdata/basicdataDriverArtery';
export default {
  data() {
    return {
      columnList: [
        // {
        //   prop: '',
        //   label: '序号',
        //   type: 0,
        //   values: '',
        //   width: 55,
        //   checkarr: [],
        //   fixed: true,
        // },
        // {
        //   prop: 'number',
        //   label: '编号',
        //   type: 2,
        //   values: '',
        //   width: '150',
        //   checkarr: [],
        //   fixed: true,
        //   sortable: true,
        //   head: false,
        // },
        {
          prop: 'trainNumber',
          label: '配送车次',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'stockArticleId',
          label: '订单自编号',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'warehouseName',
          label: '配送仓库',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'consignee',
          label: '客户名称',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'deliveryPhone',
          label: '客户电话',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'deliveryAddress',
          label: '客户地址',
          type: 2,
          values: '',
          width: '200',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'deliveryType',
          label: '业务类型',
          type: 3,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'reservationNum',
          label: '总件数',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'loadedNumber',
          label: '装车件数',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'receivedQuantity',
          label: '签收数',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'withoutScanning',
          label: '装车未扫数',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'unsigneds',
          label: '未签收数',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'deliveryDriverName',
          label: '送货司机',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'deliveryDriverPhone',
          label: '送货手机手机号',
          type: 1,
          values: '',
          width: '180',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'signingTime',
          label: '文员签收时间',
          type: 4,
          values: '',
          width: '150',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'sjsigningTime',
          label: '司机签收时间',
          type: 4,
          values: '',
          width: '150',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'signeeName',
          label: '签收人',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'signingStatusAll',
          label: '司机签收状态',
          type: 2,
          values: '',
          width: '150',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: 'signingStatusAll',
          label: '文员复核状态',
          type: 2,
          values: '',
          width: '150',
          checkarr: [],
          fixed: false,
          sortable: true,
          head: false,
        },
        {
          prop: '',
          label: '操作',
          type: 6,
          values: '',
          width: '200',
          checkarr: [],
          fixed: 'right',
          hide: true,
        },
        // 更多列的配置...
      ],
      columnListedit: [],
      drawerShow: false,
      height: 0,
      // 弹框标题
      title: '',
      // 是否展示弹框
      box: false,
      // 是否显示查询
      search: true,
      // 加载中
      loading: true,
      // 是否为查看模式
      view: false,
      // 查询信息
      query: {},
      //签收类型
      signingTypeData: [],
      //签收状态
      signingStatusData: [],
      //业务类型
      deliveryTypeData: [],
      //来源
      orderSourceData: [],
      // 分页信息
      page: {
        currentPage: 1,
        pageSize: 30,
        total: 40,
      },
      // 表单数据
      form: {},
      // 选择行
      selectionList: [],
      // 表单配置
      option: option,
      // 表单列表
      data: [],
      driverData: [],
    };
  },
  mounted() {
    this.queryDictionary();
    this.init();
    /**
     * 初始化获取本地缓存的编辑隐藏的列表
     *  固定搭配，不能更改
     */
    this.getMasterDriverData();
    let checkListnewarr = this.$functions.getStorage(window.location.pathname + 'checkList');
    let flexListnewarr = this.$functions.getStorage(window.location.pathname + 'flexList');
    let sortlistnewarr = this.$functions.getStorage(window.location.pathname + 'sortlist');
    if (checkListnewarr) {
      this.columnList.map(item => {
        item.head = false;
      });
      checkListnewarr.map(ite => {
        this.columnList.map(item => {
          if (ite == item.label) {
            item.head = true;
          }
        });
      });
    } else {
      let arr = [];
      this.columnList.map(item => {
        if (item.head) {
          arr.push(item.label);
        }
      });
      this.$functions.setStorage(window.location.pathname + 'checkList', arr);
    }
    if (flexListnewarr) {
      this.columnList.map(item => {
        item.fixed = false;
      });
      flexListnewarr.map(ite => {
        this.columnList.map(item => {
          if (ite == item.label) {
            if (item.type == 6) {
              item.fixed = 'right';
            } else {
              item.fixed = true;
            }
          }
        });
      });
    } else {
      let arr = [];
      this.columnList.map(item => {
        if (item.fixed) {
          arr.push(item.label);
        }
      });
      this.$functions.setStorage(window.location.pathname + 'flexList', arr);
    }
    if (sortlistnewarr) {
      this.columnList.map(item => {
        item.sortable = false;
      });
      sortlistnewarr.map(ite => {
        this.columnList.map(item => {
          if (ite == item.label) {
            item.sortable = true;
          }
        });
      });
    } else {
      let arr = [];
      this.columnList.map(item => {
        if (item.sortable) {
          arr.push(item.label);
        }
      });
      this.$functions.setStorage(window.location.pathname + 'sortlist', arr);
    }
    // this.columnListedit=JSON.parse(JSON.stringify(this.columnList))
  },
  computed: {
    ...mapGetters(['permission', 'setting']),
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  methods: {
    showdrawer(value) {
      this.drawerShow = value;
    },
    /**
     * 弹窗的勾选回调，用于更改头部数组
     * 固定搭配，只需要更换   columnList
     */
    setnewcolum(newarr, headarr, type) {
      // console.log(newarr,'+++++++++++')
      if (type == 1) {
        this.columnList = newarr;
        this.$functions.setStorage(window.location.pathname + 'checkList', headarr);
      } else if (type == 2) {
        this.columnList = newarr;
        this.$functions.setStorage(window.location.pathname + 'flexList', headarr);
      } else if (type == 3) {
        this.columnList = newarr;
        this.$functions.setStorage(window.location.pathname + 'sortlist', headarr);
      }
    },
    //查询司机信息
    getMasterDriverData() {
      return new Promise((rv, rev) => {
        getDriverList(1, 10, '').then(res => {
          const data = res.data.data;
          let po = [];
          data.records.forEach(item => {
            let a = {
              dictKey: item.id,
              dictValue: item.name,
            };
            po.push(a);
          });
          this.driverData = po;
          console.log('===============>', res.data.data);
          rv(res.data.data.records);
        });
      });
    },
    selectionsc(value) {
      console.log(value);
    },
    delectsolt(scope) {
      const { row } = scope;
      console.log(row);
    },
    editsolt(scope) {
      const { row } = scope;
      console.log('row>>>>>>>>>>>>>>>>>>>', row);
      this.$router.push({
        path: '/distribution/signfor/distributionSignforedt',
        query: {
          id: row.id,
          name: '签收查看',
        },
      });
    },
    btnsc(index, row) {
      console.log(index, row);
    },
    selectsc(index, row) {
      this.query[row.prop] = index;
      if (!index) {
        delete this.query[row.prop];
      }
      this.onLoad(this.page);
    },
    timesc(index, row) {
      if (!!index) {
        index = dayjs(index).format('YYYY-MM-DD');
      }
      this.query[row.prop] = index;
      if (!index) {
        delete this.query[row.prop];
      }
      this.onLoad(this.page);
    },
    inputsc(index, row) {
      this.query[row.prop] = index;
      if (!index) {
        delete this.query[row.prop];
      }
      this.onLoad(this.page);
    },
    queryDictionary() {
      // getDictionaryBiz('distribution_signfor_status').then(res => {
      //   this.signingTypeData = res.data.data;
      // });
      // getDictionaryBiz('order_source').then(res => {
      //   this.orderSourceData = res.data.data;
      //   this.columnList[6].checkarr =res.data.data.map(item=>{
      //     item.value=item.dictKey
      //     item.label=item.dictValue
      //     return item
      //   });
      // });
      getDictionaryBiz('distribution_signfor_status').then(res => {
        this.signingStatusData = res.data.data;
      });
      getDictionaryBiz('distribution_type').then(res => {
        this.deliveryTypeData = res.data.data;
        this.columnList[4].checkarr = res.data.data.map(item => {
          item.value = item.dictKey;
          item.label = item.dictValue;
          return item;
        });
        this.onLoad(this.page);
      });
    },
    init() {
      this.height = this.setPx(document.body.clientHeight - 340);
    },
    searchHide() {
      this.search = !this.search;
    },
    searchChange() {
      if (this.query.businessType) {
        let businessType = this.query.businessType;
        switch (businessType) {
          case '商配':
            this.query.deliveryType = '1';
            break;
          case '市配':
            this.query.deliveryType = '2';
            break;
        }
      }
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    handleSubmit() {
      if (!this.form.id) {
        add(this.form).then(() => {
          this.box = false;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
      } else {
        update(this.form).then(() => {
          this.box = false;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
      }
    },
    handleAdd() {
      this.title = '新增';
      this.form = {};
      this.box = true;
    },
    handleEdit(row) {
      this.title = '编辑';
      this.box = true;
      getDetail(row.id).then(res => {
        this.form = res.data.data;
      });
    },
    handleView(row) {
      this.title = '查看';
      this.view = true;
      this.box = true;
      getDetail(row.id).then(res => {
        this.form = res.data.data;
      });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.selectionClear();
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    beforeClose(done) {
      done();
      this.form = {};
      this.view = false;
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      // this.$refs.table.clearSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.query.signingStatus = '2';
      getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        // eslint-disable-next-line no-empty
        for (let i = 0; i < data.records.length; i++) {
          // eslint-disable-next-line no-empty
          for (let j = 0; j < this.deliveryTypeData.length; j++) {
            // eslint-disable-next-line no-empty
            if (data.records[i].deliveryType == this.deliveryTypeData[j].dictKey) {
              data.records[i].deliveryType = this.deliveryTypeData[j].dictValue;
            }
          }
          // for (let j = 0; j < this.orderSourceData.length; j++) {
          //   // eslint-disable-next-line no-empty
          //   if (data.records[i].orderSource == this.orderSourceData[j].dictKey) {
          //     data.records[i].orderSource = this.orderSourceData[j].dictValue;
          //   }
          // }
          for (let j = 0; j < this.signingStatusData.length; j++) {
            // eslint-disable-next-line no-empty
            if (data.records[i].driverSigning == this.signingStatusData[j].dictKey) {
              data.records[i].driverSigning = this.signingStatusData[j].dictValue;
            }
          }
        }
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        // this.selectionClear();
      });
    },
  },
};
</script>
<style scoped lang="scss">
.fr-fm {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .fr-fo {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      width: 208px;
    }
  }
  .el-btn {
    margin-right: 0;
    width: auto;
  }
}
.avue-crud__right {
  display: flex;
  align-items: flex-end;
}
</style>
