<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               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 slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.bussinessuserinfo_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="registerStatus" slot-scope="{row}">
        <el-tag plain type="warning" size="small" v-if="row.registerStatus === 100">资料完善中</el-tag>
        <el-tag plain type="primary" size="small" v-if="row.registerStatus === 98">送审中</el-tag>
        <el-tag plain type="warning" size="small" v-if="row.registerStatus === 102">审核中</el-tag>
        <el-tag plain type="success" size="small" v-if="row.registerStatus === 200">审核成功</el-tag>
        <el-tag plain type="danger" size="small" v-if="row.registerStatus === 400">审核失败</el-tag>
        <el-tag plain type="danger" size="small" v-if="row.registerStatus === 99">送审失败</el-tag>
      </template>
      <!-- 操作菜单 -->
      <template slot-scope="{row}" slot="menu">
        <el-button style="color:green;" type="text" v-if="row.registerStatus != null && row.registerStatus !== 98 && row.registerStatus !== 102 && row.registerStatus !== 200" size="small" plain class="none-border"
                   @click.stop="submitRegister(row.id)">提交申请
        </el-button>
        <el-button style="color:orange;"type="text" size="small" plain class="none-border" @click.stop="handleRegisterRequestView(row.id)">进件记录</el-button>
        <el-button v-if="permission.bussinessuserinfo_withdraw" style="color:red;"type="text" size="small" plain class="none-border" @click.stop="handleWithdrawView(row.id)">账单记录</el-button>
      </template>
    </avue-crud>
    <el-dialog title="用户进件申请记录" :append-to-body="true" :visible.sync="registerRequestVisible" top="1vh" @close="closeRequestDialog" :close-on-click-modal="false">
      <bussiness-register-request :user-info-id="userInfoId" v-if="registerRequestVisible"></bussiness-register-request>
    </el-dialog>
    <el-dialog title="用户账单记录" :append-to-body="true" :visible.sync="withdrawVisible" top="1vh" @close="closeWithdrawDialog" :close-on-click-modal="false" width="80%">
      <bussiness-user-withdraw :user-info-id="userInfoId" v-if="withdrawVisible"></bussiness-user-withdraw>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/bussinessmng/bussinessuserinfo";
  import {mapGetters} from "vuex";
  import {userRegister} from "@/api/bussinessmng/bussinessregisterrequest";
  import bussinessRegisterRequest from "../bussinessmng/bussinessregisterrequest.vue"
  import bussinessUserWithdraw from "../bussinessmng/bussinessuserwithdraw.vue"

  export default {
    components: {
      bussinessRegisterRequest, bussinessUserWithdraw
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        userInfoId: null,
        registerRequestVisible: false,
        withdrawVisible: false,
        isLoading: false,
        selectionList: [],
        option: {
          height:'auto',
          menuWidth: 300,
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          delBtn: false,
          selection: true,
          dialogClickModal: false,
          group:[
            {
              label: '主体信息',
              collapse:true,
              prop: "merchantSubjectInfo",
              column: [ {
                label: "用户名称",
                prop: "userName",
                hide: true,
                rules: [{
                  required: true,
                  message: "请输入用户名称",
                  trigger: "blur"
                }]
              },
                {
                  label: "用户简称",
                  prop: "userShortName",
                  rules: [{
                    required: true,
                    message: "请输入用户简称",
                    trigger: "blur"
                  }]
                },]
            },
            {
              label: '法人信息',
              collapse: true,
              prop: "merchantCorporationInfo",
              column: [
                {
                  label: "证件类型",
                  prop: "userLicenceType",
                  type: "select",
                  span: 8,
                  dicData:[
                    {
                      label:"身份证",
                      value:"ID_CARD"
                    },
                    {
                      label:"护照",
                      value: "PASSPORT"
                    },
                    {
                      label:"港澳居民往来内地通行证",
                      value: "HM_VISITORPASS"
                    },
                    {
                      label:"台胞证",
                      value: "TAIWAN"
                    },
                    {
                      label:"士兵证",
                      value: "SOLDIER"
                    },
                    {
                      label:"军官证",
                      value: "OFFICERS"
                    }
                  ],
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请输入用户证件号",
                    trigger: "blur"
                  }]
                },
                {
                  label: "证件号",
                  prop: "userLicenceNo",
                  span: 8,
                  labelWidth: 100,
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请输入用户证件号",
                    trigger: "blur"
                  }]
                },
                {
                  label: "手机号",
                  prop: "userPhone",
                  span: 8,
                  labelWidth: 100,
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请输入银行留手机号码",
                    trigger: "blur"
                  }]
                },
                {
                  label: "法人证件正面照",
                  prop: "userCardFrontUrl",
                  type: "upload",
                  hide:true,
                  labelWidth: 130,
                  listType: "picture-img",
                  action: '/api/putObject',
                  propsHttp: {
                    url: 'data'
                  },
                  rules: [{
                    required: true,
                    message: "请上传法人证件正面照",
                    trigger: "blur"
                  }]
                },
                {
                  label: "法人证件反面照",
                  prop: "userCardBackUrl",
                  type: "upload",
                  hide:true,
                  labelWidth: 130,
                  listType: "picture-img",
                  action: '/api/putObject',
                  propsHttp: {
                    url: 'data'
                  },
                  rules: [{
                    required: true,
                    message: "请输入用户证件反面照",
                    trigger: "blur"
                  }]
                },
              ],
            },
            {
              label: '经营地址信息',
              collapse:true,
              prop: "businessAddressInfo",
              column: [
                {
                  label: "经营省",
                  prop: "province",
                  type: "select",
                  filterable: true,
                  dicUrl: "/api/cyzh-region/region/getProvinceList",
                  cascaderItem: ['city', 'district'],
                  props:{
                    label: "provinceName",
                    value: "provinceCode"
                  },
                  span: 8,
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请选择经营省",
                    trigger: "blur"
                  }]
                },
                {
                  label: "经营市",
                  prop: "city",
                  type: "select",
                  span: 8,
                  hide: true,
                  filterable: true,
                  cascaderIndex:0,
                  dicUrl: "/api/cyzh-region/region/getCityList?provinceCode={{province}}",
                  props:{
                    label: "cityName",
                    value: "cityCode"
                  },
                  rules: [{
                    required: true,
                    message: "请选择经营市",
                    trigger: "blur"
                  }]
                },
                {
                  label: "经营区",
                  prop: "district",
                  type: "select",
                  span: 8,
                  hide: true,
                  cascaderIndex:0,
                  dicUrl: "/api/cyzh-region/region/getDistrictList?cityCode={{city}}",
                  props:{
                    label: "districtName",
                    value: "districtCode"
                  },
                  rules: [{
                    required: true,
                    message: "请选择经营区",
                    trigger: "blur"
                  }]
                },
                {
                  label: "经营地址",
                  prop: "userAddress",
                  span: 24,
                  row: true,
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请输入经营地址",
                    trigger: "blur"
                  }]
                },
              ]
            },
            {
              label: '结算账户信息',
              collapse:true,
              prop: "accountInfo",
              column: [
                {
                  label: "银行账户类型",
                  prop: "bankAccountType",
                  type: "select",
                  labelWidth: 110,
                  span:8,
                  hide: true,
                  dicData:[
                    {
                      label:"借记卡",
                      value:"DEBIT_CARD"
                    },
                    {
                      label:"存折",
                      value: "PASSBOOK"
                    },
                  ],
                  rules: [{
                    required: true,
                    message: "请输入银行账户类型",
                    trigger: "blur"
                  }]
                },
                {
                  label: "开户总行编码",
                  prop: "bankCode",
                  span:8,
                  labelWidth: 110,
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请输入开户总行编码",
                    trigger: "blur"
                  }]
                },
                {
                  label: "银行账户",
                  prop: "bankCardNo",
                  span:8,
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请输入银行账户号码",
                    trigger: "blur"
                  }]
                },
              ]
            },
          ],
          column: [
            {
              label: "用户名称",
              prop: "userName",
              search: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入用户名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属采购商",
              prop: "bussinessId",
              labelWidth: 100,
              search: true,
              searchLabelWidth: 100,
              type: "select",
              filterable: true,
              dicUrl: "/api/cyzh-guosen/bussiness/listBussiness?size=500",
              props:{
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属采购商",
                trigger: "blur"
              }]
            },
            {
              label: "进件状态",
              prop: "registerStatus",
              type: "select",
              search: true,
              slot: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=merchant_register_status",
              display: false,
              props:{
                label: "dictValue",
                value: "dictKey"
              },
            },
            {
              label: "审核意见",
              prop: "auditOpinion",
              disabled: true,
              rules: [{
                required: false,
                message: "请输入审核意见",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.bussinessuserinfo_add, false),
          viewBtn: this.vaildData(this.permission.bussinessuserinfo_view, false),
          delBtn: this.vaildData(this.permission.bussinessuserinfo_delete, false),
          editBtn: this.vaildData(this.permission.bussinessuserinfo_edit, false),
          withdrawBtn: this.vaildData(this.permission.bussinessuserinfo_withdraw, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      closeRequestDialog(){
        this.registerRequestVisible = false;
      },
      closeWithdrawDialog(){
        this.withdrawVisible = false;
      },
      handleRegisterRequestView(userInfoId){
        this.userInfoId = userInfoId;
        this.registerRequestVisible = true;
      },
      handleWithdrawView(userInfoId){
        this.userInfoId = userInfoId;
        this.withdrawVisible = true;
      },
      submitRegister(userInfoId) {
        this.$confirm("确定提交进件申请吗?如果需要修改，请先点击保存后再提交申请", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          userRegister({"userInfoId": userInfoId}).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.onLoad(this.page);
          });
        });
      },
      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();
          });
      },
      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;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
