<template>
  <basic-container>
    <avue-crud :option="option"
               v-model:search="search"
               v-model:page="page"
               v-model="form"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button type="danger"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.basicdataDriverArtery_delete"
                   @click="handleDelete">删 除
        </el-button>
<!--        <el-button type="warning"-->
<!--                   plain-->
<!--                   icon="el-icon-download"-->
<!--                   @click="handleExport">导 出-->
<!--        </el-button>-->
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,getPage} from "@/api/basicdata/basicdataDriverArtery";
  import { getList as getUserList,getUser} from '@/api/system/user'
  import {getVehicle } from "@/api/basicdata/basicdataVehicle";
  import option from "@/option/basicdata/basicdataDriverArtery";
  import {mapGetters} from "vuex";
  import {exportBlob} from "@/api/common";
  import {getToken} from '@/utils/auth';
  import {downloadXls} from "@/utils/util";
  import {dateNow} from "@/utils/date";
  import NProgress from 'nprogress';
  import 'nprogress/nprogress.css';
  import { getDetail as getDetailDelineNode } from '@/api/basic/basicDelineNode';
  export default {
    data() {
      return {
        form: {
        },
        query: {},
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "主键",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "租户号",
              prop: "tenantId",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "创建人",
              prop: "createUser",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "更新人",
              prop: "updateUser",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "更新时间",
              prop: "updateTime",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "状态",
              prop: "status",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "是否已删除;1-未删除，2-已删除",
              prop: "isDeleted",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "创建部门",
              prop: "createDept",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "用户",
              prop: "userId",
              type: "select",
              labelWidth:'150',
              dicData: [],
              width:"100px",
              align:'center',
              span:11,
              hide: true,
              filterable: true,
              rules: [
                {
                  required: true,
                  message: '请选择用户',
                  trigger: 'blur',
                },
              ],
              change: (val) => {
                if (!!val.value && val.value !== -1){
                getUser(val.value).then(res => {
                  this.form.name =res.data.data.name
                  this.form.phone =res.data.data.phone
                })
                }

              },
            },
            // {
            //   label: "姓名",
            //   prop: "name",
            //   type: "input",
            //   search: true,
            //   labelWidth:'150',
            //   width:"100px",
            //   align:'center',
            //   span:11,
            // },
            {
              label: "手机号码",
              prop: "phone",
              type: "input",
              search: true,
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "司机类型",
              prop: "type",
              type: "select",
              search: true,
              labelWidth:'150',
              dicUrl: '/api/blade-system/dict-biz/dictionary?code=basic_driver_type',
              props: {
                label: 'dictValue',
                value: 'dictKey',
              },
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "职务类型",
              prop: "jobType",
              type: "select",
              search: true,
              labelWidth:'150',
              dicUrl: '/api/blade-system/dict-biz/dictionary?code=basic_driverjob_type',
              props: {
                label: 'dictValue',
                value: 'dictKey',
              },
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "准驾车型",
              prop: "drivingType",
              type: "select",
              labelWidth:'150',
              dicUrl: '/api/blade-system/dict-biz/dictionary?code=basic_driving_type',
              props: {
                label: 'dictValue',
                value: 'dictKey',
              },
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "备注",
              prop: "notes",
              type: "input",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "绑定车辆",
              prop: "bindVehicles",
              type: "select",
              labelWidth:'150',
              width:"200px",
              align:'center',
              span:11,
              multiple: true,
              dicData: [],
              // dicUrl: '/api/logpm-basicdata/vehicle/dictionary',
              // props: {
              //   label: 'vehicleNub',
              //   value: 'id',
              // },
              // addDisplay: false,
              // editDisplay: false,
              // viewDisplay: false,
              // hide: true,
            },
            {
              label: "合同开始时间",
              prop: "contractStartTime",
              type: "date",
              format: "YYYY-MM-DD",
              valueFormat: "YYYY-MM-DD",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "合同结束时间",
              prop: "contractEndTime",
              type: "date",
              format: "YYYY-MM-DD",
              valueFormat: "YYYY-MM-DD",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "身份证号",
              prop: "idCard",
              type: "input",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "性别",
              prop: "gender",
              type: "select",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
              dicUrl: '/api/blade-system/dict-biz/dictionary?code=sex',
              props: {
                label: 'dictValue',
                value: 'dictKey',
              },
            },
            {
              label: "居住地址",
              prop: "residentialAddress",
              type: "input",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "银行类型",
              prop: "bankType",
              type: "select",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
              dicUrl: '/api/blade-system/dict-biz/dictionary?code=basic_bank_type',
              props: {
                label: 'dictValue',
                value: 'dictKey',
              },
            },
            {
              label: "银行卡号",
              prop: "bankCardNub",
              type: "input",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "驾驶证号",
              prop: "driverLicenseNub",
              type: "input",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "驾驶证发证机关",
              prop: "driverLicenseOrgan",
              type: "input",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "驾驶证起始日期",
              prop: "driverLicenseStartTime",
              type: "date",
              format: "YYYY-MM-DD",
              valueFormat: "YYYY-MM-DD",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "驾驶证到期日期",
              prop: "driverLicenseEndTime",
              type: "date",
              format: "YYYY-MM-DD",
              valueFormat: "YYYY-MM-DD",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "从业资格证编号",
              prop: "employeeQualificationNub",
              type: "input",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "道路经营许可证号",
              prop: "roadOperationLicenseNub",
              type: "input",
              labelWidth:'150',
              width:"150px",
              align:'center',
              span:11,
            },
            {
              label: "承运商",
              prop: "carrierId",
              type: "select",
              labelWidth:'150',
              width:"100px",
              align:'center',
              search: true,
              span:11,
              filterable: true,
              dicUrl: '/api/logpm-basicdata/carrier/dictionary',
              props: {
                label: 'number',
                value: 'id',
              },
              rules: [{ required: true, message: '请选择承运商!',trigger: 'blur'}],
            },
            {
              label: "运输协议",
              prop: "transportationAgreement",
              type: "input",
              labelWidth:'150',
              width:"100px",
              align:'center',
              span:11,
            },
            {
              label: "人证合照照片",
              prop: "licensePeoplePhoto",
              type: "upload",
              dataType: 'string',
              listType: 'picture-img',
              hide: true,
              labelWidth:'150',
              span:11,
              multiple:false,
              width:"200px",
              align:'left',
              action: '/blade-resource/oss/endpoint/put-file',
              propsHttp: {
                res: 'data',
                url: 'link',
              },
            },
            {
              label: "身份证正面照片",
              prop: "idCardFrontPhoto",
              type: "upload",
              dataType: 'string',
              listType: 'picture-img',
              hide: true,
              labelWidth:'150',
              span:11,
              multiple:false,
              width:"200px",
              align:'left',
              action: '/blade-resource/oss/endpoint/put-file',
              propsHttp: {
                res: 'data',
                url: 'link',
              },
            },
            {
              label: "身份证背面照片",
              prop: "idCardBackPhoto",
              type: "upload",
              dataType: 'string',
              listType: 'picture-img',
              hide: true,
              labelWidth:'150',
              span:11,
              multiple:false,
              width:"200px",
              align:'left',
              action: '/blade-resource/oss/endpoint/put-file',
              propsHttp: {
                res: 'data',
                url: 'link',
              },
            },
            {
              label: "驾驶证照片",
              prop: "driverLicensePhoto",
              type: "upload",
              dataType: 'string',
              listType: 'picture-img',
              hide: true,
              labelWidth:'150',
              span:11,
              multiple:false,
              width:"200px",
              align:'left',
              action: '/blade-resource/oss/endpoint/put-file',
              propsHttp: {
                res: 'data',
                url: 'link',
              },
            },
            {
              label: "驾驶证背面照片",
              prop: "driverLicensePhotoBack",
              type: "upload",
              dataType: 'string',
              listType: 'picture-img',
              hide: true,
              labelWidth:'150',
              span:11,
              multiple:false,
              width:"200px",
              align:'left',
              action: '/blade-resource/oss/endpoint/put-file',
              propsHttp: {
                res: 'data',
                url: 'link',
              },
            },


            {
              label: "预留1",
              prop: "reserve1",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "预留2",
              prop: "reserve2",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "预留3",
              prop: "reserve3",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "预留4",
              prop: "reserve4",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "预留5",
              prop: "reserve5",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.basicdataDriverArtery_add, false),
          viewBtn: this.validData(this.permission.basicdataDriverArtery_view, false),
          delBtn: this.validData(this.permission.basicdataDriverArtery_delete, false),
          editBtn: this.validData(this.permission.basicdataDriverArtery_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleExport() {
        let downloadUrl = `/blade-basicdataDriverArtery/basicdataDriverArtery/export-basicdataDriverArtery?${this.website.tokenHeader}=${getToken()}`;
        const {
            name,
            phone,
            type,
            jobType,
        } = this.query;
        let values = {
            name_like: name,
            phone_like: phone,
            type_equal: type,
            jobType_equal: jobType,
        };
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          NProgress.start();
          exportBlob(downloadUrl, values).then(res => {
            downloadXls(res.data, `司机信息表${dateNow()}.xlsx`);
            NProgress.done();
          })
        });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        const {
            name,
            phone,
            type,
            jobType,
          carrierId,
        } = this.query;

        let values = {
            name: name,
            phone: phone,
            type: type,
            jobType: jobType,
          carrierId: carrierId,
        };

        getPage(page.currentPage, page.pageSize, values).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          // console.log("this.option.column>>>>>>>",this.option.column);
           this.option.column[16].dicData =[]
           this.option.column[9].dicData =[]
          //查询车辆绑定情况
          this.getVehiclede();
           //查询人员信息
           this.getpeople();
          // getVehicle().then(res => {
          //   console.log("res.data.data>>>>>>>",res.data.data)
          //    res.data.data.map(v=>{
          //      let value =
          //        {
          //        label: v.vehicleNub,
          //        value: v.id,
          //        disabled: true
          //      }
          //      if (v.vehicleOwner ==1){
          //        value.disabled =false
          //      }
          //      this.option.column[15].dicData.push(value)
          //
          //    })
          // });
       //   this.selectionClear();
        });
      },
      //查询车辆绑定情况
      getVehiclede(){
        getVehicle().then(res => {
          console.log("res.data.data>>>>>>>",res.data.data)
          res.data.data.map(v=>{
            let value =
              {
                label: v.vehicleNub,
                value: v.id,
              }
            this.option.column[16].dicData.push(value)
          })
        });
      },
      //查询人员绑定情况
      getpeople(){
        getUserList(
          1,
          100000,
          null,
         ''
        ).then(res => {
          const data = res.data.data;
          data.records.map(v=>{
            let value =
              {
                label: v.name,
                value: v.id,
              }
            this.option.column[9].dicData.push(value)
          })
        });
      }
    }
  };
</script>

<style>
</style>
