<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="primary"-->
<!--                   size="small"-->
<!--                   icon="el-icon-plus"-->
<!--                   plain-->
<!--                   v-if="permission.bills_add"-->
<!--                   @click="addBills">新 增-->
<!--        </el-button>-->
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.bills_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
<!--      <template slot-scope="{row}" slot="inputForm">-->
<!--        <el-tag>{{row}}</el-tag>-->
<!--      </template>-->
<!--      <template slot-scope="{row}" slot="buttonForm">-->
<!--        <el-button @click="addAll" size="small" type="primary">添加10条子表单数据</el-button>-->
<!--      </template>-->
    </avue-crud>
<!--    <el-dialog title="自定义新增" :visible.sync="billsAdd" :modal-append-to-body="false">-->
<!--      <BillsAdd v-if="billsAdd"></BillsAdd>-->
<!--      <BillsItem v-if="billsAdd"></BillsItem>-->
<!--    </el-dialog>-->

  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/estate/bills";
  // import BillsAdd from "@/components/bills-comp/bills-add";
  // import BillsItem from "@/components/bills-comp/bills-item";
  import {mapGetters} from "vuex";

  export default {
    // components:{BillsAdd,BillsItem},
    data() {
      return {
        billsAdd:false,
        form: {},
        query: {},
        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,
          // addBtn:false,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          menuWidth:350,
          column: [
            // {
            //   label: "所属社区",
            //   prop: "agencyId",
            //   // slot: true,
            //   search: true,
            //   cascaderItem: ['residentialId'],
            //   type: "select",
            //   // remote: true,
            //   searchFilterable:true,
            //   dicUrl: "/api/cyzh-community/agency/list?size=1000&name={{key}}",
            //   dicFormatter:(res)=>{
            //     return res.data.records;//返回字典的层级结构
            //   },
            //   props: {
            //     label: "name",
            //     value: "id"
            //   },
            //   rules: [{
            //     required: true,
            //     message: "请选择所属社区",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "所属小区",
              prop: "residentialId",
              // slot: true,
              type: "select",
              search: true,
              cascaderItem: ['buildingId'],
              dicUrl: "/api/cyzh-community/residential/list?agencyId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              // slot: true,
              type: "select",
              search: true,
              cascaderItem: ['unitId'],
              dicUrl: `/api/cyzh-community/building/list?residentialId={{key}}`,
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入楼栋Id",
                trigger: "blur"
              }]
            },
            {
              label: "所属单元",
              prop: "unitId",
              // slot: true,
              type: "select",
              search: true,
              cascaderItem: ['floorId'],
              dicUrl: "/api/cyzh-community/unit/list?buildingId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入单元Id",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼层",
              prop: "floorId",
              // slot: true,
              type: "select",
              search: true,
              cascaderItem: ['roomId'],
              dicUrl: "/api/cyzh-community/floor/list?unitId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入楼层Id",
                trigger: "blur"
              }]
            },
            {
              label: "所属房间",
              prop: "roomId",
              // slot: true,
              type: "select",
              search: true,
              dicUrl: "/api/cyzh-community/room/list?floorId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入房间Id",
                trigger: "blur"
              }]
            },
            {
              label: "账单日期",
              prop: "billsDate",
              type:'date',
              valueFormat:'yyyy-MM-dd',
              rules: [{
                required: true,
                message: "请输入账单日期",
                trigger: "blur"
              }]
            },
            {
              label: "账单总费用(元)",
              prop: "totalFee",
              hide:true,
              display:false,
              rules: [{
                required: true,
                message: "请输入账单总费用",
                trigger: "blur"
              }]
            },
            {
              label: "物业公司",
              prop: "estateCompany",
              rules: [{
                required: true,
                message: "请输入物业公司",
                trigger: "blur"
              }]
            },
            // {
            //   label: "物业公司Id",
            //   prop: "estateCompanyId",
            //   hide:true,
            //   rules: [{
            //     required: true,
            //     message: "请输入物业公司Id",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "缴费方式",
              prop: "payWay",
              type:"select",
              dicData: [
                {
                  label:'微信支付',
                  value:1
                },{
                  label:'其他方式',
                  value:2
                }
              ],
              rules: [{
                required: true,
                message: "请输入缴费方式",
                trigger: "blur"
              }]
            },
            // {
            //   label: "支付流水",
            //   prop: "payId",
            //   rules: [{
            //     required: true,
            //     message: "请输入支付流水",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "支付状态",
              prop: "payStatus",
              type:'select',
              search:true,
              dicData:[
                {
                  label: '未支付',
                  value: 1
                },{
                  label: '已支付',
                  value: 2
                }
              ],
              rules: [{
                required: true,
                message: "请选择支付状态",
                trigger: "blur"
              }]
            },
            {
              label: '缴费项',
              prop: 'billsItemList',
              type: 'dynamic',
              span:24,
              children: {
                align: 'center',
                headerAlign: 'center',
                rowAdd:(done)=>{
                  // this.$message.success('添加缴费项成功');
                  done({
                    // input:'默认值'
                  });
                },
                rowDel:(row,done)=>{
                  // this.$message.success('删除回调'+JSON.stringify(row));
                  done();
                },
                column: [{
                  label: '缴费项名称',
                  prop: "itemName",
                },{
                  label: '缴费金额',
                  prop: "itemFee",
                  type: 'number'
                },{
                  label: '备注',
                  prop: "itemDesc",
                }
                // ,{
                //   label:'',
                //   hide:true,
                //   display:false,
                //   prop:"isDeleted"
                // }
                ]
              }
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.bills_add, false),
          viewBtn: this.vaildData(this.permission.bills_view, false),
          delBtn: this.vaildData(this.permission.bills_delete, false),
          editBtn: this.vaildData(this.permission.bills_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      // rowAdd(){
      //   this.billsAdd = true;
      // },
      // addBills(){
      //   this.billsAdd = true;
      // },
      // addAll(){
      //   for(let i=0;i<10;i++){
      //     this.form.dynamic.push({
      //       // input: 1,
      //       // select: 1,
      //       // radio: 1,
      //     })
      //   }
      // },

      rowSave(row, done, loading) {
        row.agencyName = row.$agencyId;
        row.residentialName = row.$residentialId;
        row.buildingName = row.$buildingId;
        row.unitName = row.$unitId;
        row.floorName = row.$floorId;
        row.roomName = row.$roomId;
        console.log(row,"新增行数据");
        if (row.billsItemList.length===0){
          this.$confirm("请输入缴费项！", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
        }
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        row.agencyName = row.$agencyId;
        row.residentialName = row.$residentialId;
        row.buildingName = row.$buildingId;
        row.unitName = row.$unitId;
        row.floorName = row.$floorId;
        row.roomName = row.$roomId;
        console.log(row,"修改数据");
        if (row.billsItemList.length===0){
          this.$confirm("请输入缴费项！", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
        }
        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>
